Jetpack Compose - 停止使用键盘调整背景大小

问题描述 投票:0回答:3

我已将

android:windowSoftInputMode="adjustResize"
添加到 AndroidManifest.xml 文件中,以允许文本字段、按钮和文本向上移动,但背景也会自行调整大小。我怎样才能阻止这种情况发生?

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {

            //Used to determine where the starting launch point of the app is
            val user by remember { mutableStateOf(Firebase.auth.currentUser) }
            AppNavigation(
                startDestination = if (user == null) {
                    ViewToDisplayKeys.SignInOrSignUp.toString()
                } else {
                    ViewToDisplayKeys.OnboardingStart.toString()
                }
            )

        }
    }
}



@Composable
fun AppNavigation(
    navController: NavHostController = rememberNavController(),
    startDestination: String = "signInOrSignUp"
) {
    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        composable("signInOrSignUp") {
            SignInOrSignUpMasterView(navController = navController)
        }
        composable("onboarding") {
            OnboardingStartView(navController)
        }
    }
}   

android-jetpack-compose android-softkeyboard android-jetpack android-background
3个回答
2
投票

放在onCreate方法中,代码如下:

class SignInOrSignUp : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getWindow().setBackgroundDrawableResource(R.drawable.sign_in_sign_up_background)
        setContent {
            AppTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    SignInOrSignUpMasterView()
                }
            }

        }

    }
}


@Composable
fun SignInOrSignUpView() {

    Box {

        //Background image
        /*Image(
            painter = painterResource(id = R.drawable.sign_in_sign_up_background),
            contentDescription = null,
            contentScale = ContentScale.FillBounds,
            alpha = 0.50F,
            modifier = Modifier
                .background(Color.White)
        )*/


        SignUpView()
    }

}

1
投票

您需要将背景设置为窗口:

getWindow().setBackgroundDrawableResource(R.drawable.sign_in_sign_up_background)

0
投票

我在 imePadding() 中遇到了类似的问题,当我聚焦文本字段时,背景大小发生了调整。

对我有用的解决方案是确保 imePadding() 不适用于背景图像。所以类似:

val scrollState = rememberScrollState()

BoxWithConstraints(modifier = Modifier.fillMaxSize()){
      Image(painter = painterResource(id = R.background),
            contentScale = ContentScale.FillBounds,
            modifier = Modifier.fillMaxSize())
      Column(
        horizontalAlignment = Alignment.CenterHorizontally, 
        verticalArrangement = Arrangement.Bottom,
        modifier = Modifier.fillMaxWidth()
                           .verticalScroll(scrollState)
                           .imePadding() 
       {
          //inside this column add the content you want to scroll
          //text fields, buttons, etc.
       }
}

根据需要修改horizontalAlignment和verticalArrangement。

请注意,Modifier.fillMaxSize() 无法与 VerticalScroll 修饰符一起使用(请参阅:fillMaxSize 修饰符在 Jetpack Compose 中与 VerticalScroll 组合使用时不起作用),这就是为什么我对列使用 Modifier.fillMaxWidth() 的原因。

© www.soinside.com 2019 - 2024. All rights reserved.