這裏以一個簡單的Demo演示下
界面如下所以
界面的XML如下所示
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是頂部的Button"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/edit_text"
android:text="我是底部的Button"/>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="我是底部的Edittext"/>
</RelativeLayout>
在點擊底部的Edittext後彈出軟鍵盤,軟鍵盤把Edittext頂上去了,沒有被遮擋,但是頂部的標題欄和按鈕卻被頂到了佈局外看不見了,如下gif所示。這樣的體驗其實也不好,因爲用戶在輸入的同時,有可能要操作標題欄上的ActionButton,但是此時標題已被頂出佈局外,用戶還要先收起軟鍵盤,再去操作標題欄上的按鈕。
這時候就該adjustResize上場了,我們可以在Manifest中設置Activity的android:windowSoftInputMode屬性值爲"adjustResize",如下代碼所示
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
</activity>
設置完的運行效果如gif圖所示
Perfect,正是我們想要的效果,底部的內容被輸入法往上頂,輸入的時候不會被遮擋,頂部的標題欄又不會被頂出佈局。