1. 程式人生 > >Android EditText彈出軟鍵盤實現頁面標題頭不動,軟鍵盤彈出在編輯框下面

Android EditText彈出軟鍵盤實現頁面標題頭不動,軟鍵盤彈出在編輯框下面

為了實現EditText編輯的時候彈出軟鍵盤標題頭不動,底部編輯框上移,想了好多種方法,也百度,問同事每種辦法都有問題,在這總結一下,希望能幫助到大家。
上圖看下效果:
彈出前
彈出後
可以看到彈出鍵盤的時候,只有EditText在軟鍵盤上面,還有一個藍色點,這個隨後再說。
用RelativeLayout實現的父佈局id為root,試驗過很多種方法,總結一下,想要實現這種效果,有一個原則 EditText所在的佈局中,必須位於root的底部,也就是寫android:layout_alignParentBottom=”true”這個屬性,然後再根據具體情況調整佈局。
為了更簡單的實現功能,頁面算是最簡單的實現了,現在說一下那個藍色的點,他是位於EditText上面也就是用了above屬性,所以我們可以看到它也被彈到佈局上面。
總結一下規律就是,RelativeLayout在彈出軟鍵盤的時候先尋找android:layout_alignParentBottom屬性是否有控制元件設定為true,如果有將此控制元件向上移動鍵盤高度的位置,佈局也就位於軟鍵盤的上面,其他控制元件如果有相對於該控制元件的位置,也就相對的移動了,如果沒有則什麼都不做,可以看做佈局是一層一層蓋上去的,鍵盤彈出的時候,只把符合要求的當層的佈局向上移動,

所以如果我們按照這種方法寫,肯定是可以的。
還有一個重點就是,配置檔案裡面該activity要設定android:windowSoftInputMode=”adjustResize”
相信還有小夥伴會遇到設定之後不生效的結果,那就在佈局檔案的根佈局新增android:fitsSystemWindows=”true”屬性,但是有的還會有問題,如果程式碼中有設定狀態列顏色的,會多出一條狀態列高度的空白條,這個還不知道如何解決。
也是經過很多實驗發現的,表述不是很清楚,不懂得可以溝通,希望可以幫助到大家吧,畢竟感覺這個坑不大不小,但卻挺煩人的。有大神也可以給出更權威的答案,相互學習,共同進步。
在此附上程式碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/white">

    <include
        android:id
="@+id/ll_title_bar" layout="@layout/appbar"/>
<LinearLayout android:id="@+id/ll_center" android:layout_width="match_parent" android:layout_below="@+id/ll_title_bar" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 111" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 222" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 333" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 4444" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 555" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 6666" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 777" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test 888" android:layout_margin="10dp" android:padding="10dp" android:background="@color/error_stroke_color" /> </LinearLayout> <EditText android:id="@+id/et_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:hint="EditText" android:background="@color/blue_btn_bg_color" android:padding="10dp" /> <ImageView android:layout_width="10dp" android:layout_height="10dp" android:layout_above="@+id/et_test" android:layout_centerHorizontal="true" android:background="@color/colorPrimary"/> </RelativeLayout>

大家可以改一下佈局,會發現只有藍色的點向上移動了
正常的程式碼