Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG
16lz
2021-01-23
一.前言
在开发中,如果输入框在布局的底部。在弹出输入发时,为了使输入法不遮挡输入框通常有两种做法:
1.将布局压缩(Activity的android:windowSoftInputMode属性设置为”adjustResize”)。
2.移动布局,将布局顶到输入框之上(Activity的android:windowSoftInputMode属性设置为”adjustPan”)
在使用沉浸式状态栏之后,发现将布局压缩的方法没用了(Activity的android:windowSoftInputMode属性设置为”adjustResize”了),但是移动布局的方式还是有用的。
二.解决方法
不知道这是不是Android的一个BUG,找了很多资料,才发现有以下一种解决方法。
1.自定义ViewGroup(LinearLayout,RelativeLayout等),重写fitSystemWindows方法,如下:
public class MyLinearLayout extends LinearLayout { public MyLinearLayout(Context context) { super(context); } public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected boolean fitSystemWindows(Rect insets) { insets.top = 0; return super.fitSystemWindows(insets); }}
2.将原先的xml布局的根ViewGroup换成我们自定义的ViewGroup。
3.在Activity或Fragment中加载出该自定义ViewGroup,然后调用setFitsSystemWindows(true)方法,如下:
MyLinearLayout linearLayout = (MyLinearLayout) findViewById(R.id.activityMain_mRootView);linearLayout.setFitsSystemWindows(true);
最好在Activity或Fragment销毁时调用linearLayout.setFitsSystemWindows(false);
至此完毕,如有错误,欢迎指教。
更多相关文章
- Unity3D和Android之间的方法交互(jar模式)
- Android之LocationManager类的方法详解
- android init.rc 修改方法---adb shell 不能修改的原因
- android:configChanges属性在不同版本SDK下需要注意的事项
- 浅谈Android五大布局——LinearLayout、FrameLayout, AbsoulteLa