關(guān)于Adnroid的軟鍵盤
今天在做聊天輸入框的時候僻肖,發(fā)現(xiàn)彈出的軟鍵盤把獲得焦點的EditText
控件給擋住了肖爵,于是找了相關(guān)的API。官方提供了一系列的配置參數(shù)可供選擇臀脏。在清單文件中對應(yīng)的Activity
中的windowSoftInputMode
屬性選擇
SOFT_INPUT_ADJUST_NOTHING
SOFT_INPUT_ADJUST_PAN
SOFT_INPUT_ADJUST_RESIZE
-
SOFT_INPUT_ADJUST_UNSPECIFIED
<activity android:windowSoftInputMode="stateHidden|adjustNothing" android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
如上配置即可劝堪。
-
adjustNothing
不調(diào)整; -
adjustPan
會將整個頁面都頂上去揉稚,為了把輸入框顯示出來秒啦; -
adjustResize
會重新調(diào)整壓縮頁面的尺寸,把軟鍵盤需要的空間讓出來搀玖,看起來就像被壓扁了余境; -
adjustUnspecified
作為一種默認(rèn)的配置,系統(tǒng)自己根據(jù)內(nèi)容自行選擇上兩種方式的一種執(zhí)行。
主要就是adjustPan
和adjustResize
之間選擇了芳来,可是兩種都達不到預(yù)期的效果含末,達不到當(dāng)軟鍵盤彈出時背景不變形,位置不變化即舌,只是EditText
的位置始終在軟鍵盤的頂部佣盒,彈出的時候把背景給蓋住了〗暮看起來就像是兩部分沼撕,輸入框在動宋雏,可以把背景給蓋住芜飘。
最后,還是把布局分為了兩部分磨总,背景部分用一個ScrollView
包裹了起來嗦明,這樣就不會因為軟鍵盤的彈出,一會兒被壓扁或者被頂上去了蚪燕。頂上去的只有EditText
這個輸入框娶牌,windowSoftInputMode
模式還是選擇adjustResize
這個,用adjustPan
的話馆纳,背景還是會被頂上去诗良,不管是不是加了ScrollView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a"/>
</LinearLayout>
</ScrollView>
<EditText
android:id="@+id/mEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
這樣就達到預(yù)期的效果了
還有美女^^