Android待办App使用ListView时软键盘弹出列表上移顶部元素被遮挡如何调整
解决方法
1. 配置软键盘页面响应规则
打开AndroidManifest.xml,找到承载待办页面的Activity节点,添加软键盘模式属性:android:windowSoftInputMode="adjustNothing"
这个设置会禁止系统在唤起软键盘时自动调整整个页面的布局偏移,从根源上避免ListView整体上移。
注意:如果设置后发现软键盘完全遮挡底部输入框,可以把属性值换成
adjustResize,这种模式下系统只会给布局腾出软键盘占用的屏幕空间,不会向上推送所有控件。
2. 修正ListView的布局冲突配置
现有布局里的ListView存在和约束逻辑冲突的参数,是触发偏移的次要原因,把ListView的属性替换为如下配置:
<ListView android:id="@+id/listView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="32dp" android:layout_marginEnd="32dp" android:layout_marginRight="32dp" android:layout_marginBottom="16dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/editText" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
需要删掉的冲突配置:
- 写死的固定高度
android:layout_height="450dp" - 会跟随布局空间变化偏移位置的垂直偏量
app:layout_constraintVertical_bias="0.403" - 不符合ListView滚动容器属性的
android:isScrollContainer="false"
修改后ListView的顶部会始终固定在屏幕上方的指定位置,软键盘弹出压缩可用空间时,只会缩小ListView本身的可视高度,不会把已经显示在顶部的列表条目挤出可视范围。
问题根源
软键盘弹出时,系统默认的布局调整逻辑会把整个页面内容向上推送,导致ListView顶部的内容滑出屏幕可视区域;等列表累积3-4条数据后,ListView自动滚动定位到底部,才会把之前被遮挡的条目拉回可视范围,出现“要加几个条目才能看到全部内容”的异常表现。
内容的提问来源于stack exchange,提问作者Ali Yusifov
相关产品推荐
相关产品推荐

