一.前言

在开发中,如果输入框在布局的底部。在弹出输入发时,为了使输入法不遮挡输入框通常有两种做法:
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);

至此完毕,如有错误,欢迎指教。

更多相关文章

  1. Unity3D和Android之间的方法交互(jar模式)
  2. Android之LocationManager类的方法详解
  3. android init.rc 修改方法---adb shell 不能修改的原因
  4. android:configChanges属性在不同版本SDK下需要注意的事项
  5. 浅谈Android五大布局——LinearLayout、FrameLayout, AbsoulteLa

随机推荐

  1. Dojo mobile TweetView 系列教程之五 —
  2. 2011.09.26(2)——— android sample之Note
  3. android 常用的数据库表以及操作说明
  4. 4.11笔记 android database打开方式,ios反
  5. android用户界面-组件Widget-画廊视图Gal
  6. Android动态显示隐藏密码输入框的内容
  7. Android(安卓)IPC 通讯机制源码分析
  8. 安卓入门.RelativeLayout相对布局1
  9. android实战
  10. android实现页面下方的Tab效果