使用EditText的时候难免会出现输入法弹出遮挡住部分控件的情况,这样让用户体验很不好。那么如果需要输入法弹出时将部分控件顶上去又保证整体界面不上移,在这里实践总结出了一套方法。
先看效果:
那么,接下来动手吧:
1.在AndroidManifest.xml文件中,往需要实现这个效果所在的Activity添加android:windowSoftInputMode="stateHidden|adjustResize"
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2.在Activity对应的layout文件中,在父布局中添加
android:fitsSystemWindows="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
...
...
...
</RelativeLayout>
就可以啦,简单两步!
搭配沉浸式状态栏
当然,以上两步还是适用于没有使用沉浸式状态栏的情况,如果你的demo使用了沉浸式状态栏 的话,可以将对应layout文件中的父布局改成fitSystemLayout
<?xml version="1.0" encoding="utf-8"?>
<fitSystemLayout xmlns:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
...
...
...
</RelativeLayout>
在这里附上fitSystemLayout的类,可直接复制到你的demo里使用
public class fitSystemLay