windowSoftInputMode屬性詳解

當(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
  1. <activity> windowSoftInputMode=“adjustResize”,彈出鍵盤:
adjustResize.png

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

  1. <activity> windowSoftInputMode=“adjustPan”,彈出鍵盤:
adjustPan.png

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容