當(dāng)EditText獲取到焦點(diǎn),鍵盤彈出,為保證EditText不被遮擋,activity的布局會(huì)做一些調(diào)整,<activity>的windowSoftInputMode屬性便控制此時(shí)activity的布局如何調(diào)整。
windowSoftInputMode常用兩個(gè)值,分別是 adjustResize, adjustPan
adjustResize當(dāng)鍵盤彈出時(shí),從下往上壓縮activity布局,壓縮的距離等于鍵盤的高度。壓縮之后,不管EditText是否可見。
adjustPan當(dāng)鍵盤彈出時(shí),如果EditText被鍵盤遮擋,activity內(nèi)容從下向上滾動(dòng),以保證EditText不被鍵盤遮擋;如果EditText不被遮擋,則不滾動(dòng)。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_marginTop="300dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginTop="200dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="alignParentBottom"
android:background="@android:color/holo_blue_bright"/>
</RelativeLayout>
界面效果:

device-2017-04-21-105353.png
- <activity> windowSoftInputMode=“adjustResize”,彈出鍵盤:

adjustResize.png
鍵盤彈出,activity從下而上壓縮到鍵盤上邊空間區(qū)域大小,由于TextView是alignParentBottom底部對(duì)齊的,所以出現(xiàn)被鍵盤頂上去的效果。
如果xml中最外層布局是LinearLayout,壓縮之后,由于可見空間不足以展示所有內(nèi)容,TextView就會(huì)被鍵盤遮擋。
- <activity> windowSoftInputMode=“adjustPan”,彈出鍵盤:

adjustPan.png
鍵盤彈出,由于EditText到底部的距離小于鍵盤的高度,所以,activity的內(nèi)容整體向上滾動(dòng),以使EditText不給遮擋。
如果EditText到底部的距離大于鍵盤高度,activity內(nèi)容不會(huì)滾動(dòng)。