Android 表情面板切换键盘闪烁问题的解决
1、问题出现的原因
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:orientation="vertical">
<!--内容布局-->
<RelativeLayout
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<!--键盘表情切换布局-->
<RelativeLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="50dp">
</RelativeLayout>
<!--底部panel布局 相当于表情面板-->
<RelativeLayout
android:id="@+id/panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</LinearLayout>
大部分时候,我们如果做IM之类的应用,不可避免会书写以上的布局。没错,写完第一次运行,发现键盘弹出的时候直接挡住了整个布局的底部,这时候,我们按照网上找的方法来做:设置android:windowSoftInputMode=”adjustResize” 之后,不挡了,但是新的问题来了,键盘在消失和出现的时候,会伴随着闪烁,仔细看,是上面weigh为1的内容区域高度被压缩成了0,之后又好了。这个问题的出现主要是因为键盘状态发生改变时,布局进行了重新的调整。我在github中找到了解决的办法,但是存在bug,这个之后再说,先说说它的解决思路。
2、问题的解决
//之所以出现闪烁,就是因为在切换到键盘时,内容区域的高度被置为0了,因此,我们可以在将要切换到键盘或者关闭键盘时,固定键盘的高度。代码如下:
private void lockContentHeight() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mContentView.getLayoutParams();
params.height = mContentView.getHeight();
params.weight = 0.0F;
mHandler.removeCallbacks(runnable);
}
//然后延时200ms,之后将锁定的内容区域解锁,代码如下:
private void unlockContentHeightDelayed() {
mEditText.post(new Runnable() {
@Override
public void run() {
((LinearLayout.LayoutParams) mContentView.getLayoutParams()).weight = 1.0F;
}
});
}