问题描述
假设我有一些关于 jetpack-compose 内容的活动
Suppose I have some activity with a jetpack-compose content
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ScrollableColumn(
modifier = Modifier
.fillMaxSize()
.border(4.dp, Color.Red)
) {
val (text, setText) = remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = setText,
label = {},
modifier = Modifier
.fillMaxWidth()
)
for (i in 0..100) {
Text("Item #$i")
}
}
}
}
}
如果我要启动此活动并专注于 TextField,则会弹出一个软件键盘.
If I were to launch this activity and focus on the TextField a software keyboard would pop up.
然而,界面不会对此做出反应.ScrollableColumn 的底部边框 (.border(4.dp, Color.Red)
) 以及第 100 项 (Text("Item #$i")
) 将不可见>).
The interface, however, would not react to it. ScrollableColumn's bottom border (.border(4.dp, Color.Red)
) would not be visible, as well as 100th item (Text("Item #$i")
).
换句话说,软键盘与内容重叠.
In other words, software keyboard overlaps content.
如何让 jetpack 撰写尊重可见区域的变化(由于软件键盘)?
How can I make jetpack compose respect visible area changes (due to software keyboard)?
推荐答案
你可以使用标准的 android 程序,但我不知道是否存在 Compose 特定的方式.
You can use the standard android procedure, but I don't know if a Compose specific way exists.
如果您将 SoftInputMode 设置为 SOFT_INPUT_ADJUST_RESIZE
,则布局将在键盘更改时调整大小.
If you set the SoftInputMode to SOFT_INPUT_ADJUST_RESIZE
, the Layout will resize on keyboard change.
class YourActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
setContent { /* Composable Content */ }
}
}
否则,您可以使用清单中的标志.浏览此处获取更多信息:显示软键盘时向上移动布局?
otherwise, you could use the flags in the manifest. See here for more information:Move layouts up when soft keyboard is shown?
这篇关于软件键盘与 jetpack 组合视图的内容重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!