ScrollView中軟鍵盤彈出依然遮住控制元件
在一個Activity頁面中,常會有需要使用者輸入的文字控制元件EditText,EditText獲取焦點後,彈出的軟鍵盤往往會遮擋底部的其他控制元件,通常處理方法是:
1、xml佈局中,使用ScrollView包含使用者控制元件;
2、Manifest檔案相應的Activity節點下新增軟鍵盤屬性android:windowSoftInputMode=”stateHidden|adjustResize”
經過這兩步的操作,在彈出軟鍵盤時,被軟鍵盤遮擋且有ScrollView包括的控制元件在可視區域內具有滾動,被遮擋的控制元件可以通過滾動到可視區域內與使用者打交道。如下圖:
有時候為了介面美觀,我們需要將控制元件居中,或者距離頂部有一定距離位置擺放;
<RelativeLayout 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="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#CCE8CF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="1"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#CCE8CF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="4"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#CCE8CF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="5"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="6"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#CCE8CF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="7"
android:textSize="15dp" />
<EditText
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:textSize="15dp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
如上程式碼,已經佈局全部包括在ScrollView下,在ScrollView的子View下使用layout_marginTop將佈局向下偏移50dp,這時候再執行,可以發現介面最底部的EditText被軟鍵盤遮擋,即使滾動也不能移到可視區域,只能在軟鍵盤的遮擋下輸入文字,如果是Button,那隻能收縮軟鍵盤,才能夠點選;
留心的讀者也許會發現,軟鍵盤彈出後,可視區域顯示的滾動條並沒有能夠滾動到軟鍵盤頂部邊沿。
這是由於LinearLayout使用了android:layout_marginTop=”50dp”的緣故,導致ScrollView初顯的位置向下移動了50dp,解決的方法是在LinearLayout末尾空出50dp的空間,如:在LinearLayout底部新增高度為50dp的控制元件TextView或在LinearLayout新增android:paddingBottom=”50dp”
這兩種方法都可以讓最底部的可輸入文字顯示在可視區域內,但是可視區域的滾動條仍然是沒有能夠滾動到軟鍵盤底部邊沿的。