最近遇到的一個問題是全屏Activity中要求彈出鍵盤不頂起布局,首先windowSoftInputMode的取值是有多個的,在全屏場景下adjustPan是沒有用的,需要使用adjustResize首先確保鍵盤不頂起布局。
????????android:windowSoftInputMode="stateHidden|adjustResize"文章來源:http://www.zghlxwxcb.cn/news/detail-705058.html
但是單純設(shè)置windowSoftInputMode在布局方面又會有新的問題,那就是可能需要在鍵盤上方展示文本框,但是鍵盤的高度是不固定的,在全屏場景下布局不被頂起,因此文本框會被鍵盤遮擋,為解決這個問題,需要在鍵盤彈起時測量鍵盤的高度,代碼如下文章來源地址http://www.zghlxwxcb.cn/news/detail-705058.html
//在鍵盤彈起的地方對文本庫增加addOnGlobalLayoutListener,我xml的布局是ConstraintLayout
binding.etDiy.viewTreeObserver
.addOnGlobalLayoutListener {
val rect = Rect()
binding.root.getWindowVisibleDisplayFrame(rect)
val screenHeight = binding.root.height
val keypadHeight = screenHeight - rect.bottom
if (keypadHeight > screenHeight * 0.15) {
if (isSet) return@addOnGlobalLayoutListener
isSet = true
val constraintSet = ConstraintSet()
constraintSet.clone(binding.root)
constraintSet.connect(
binding.viewBackground.id,
ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID,
ConstraintSet.BOTTOM,
keypadHeight
)
constraintSet.applyTo(binding.root)
} else {
if (!isSet) return@addOnGlobalLayoutListener
isSet = false
val constraintSet = ConstraintSet()
constraintSet.clone(binding.root)
constraintSet.connect(
binding.viewBackground.id,
ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID,
ConstraintSet.BOTTOM,
0
)
constraintSet.applyTo(binding.root)
}
}
到了這里,關(guān)于全屏Activity彈出鍵盤不頂起布局的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!