我已将
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)
}
}
}
放在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()
}
}
您需要将背景设置为窗口:
getWindow().setBackgroundDrawableResource(R.drawable.sign_in_sign_up_background)
我在 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() 的原因。