應用過Android手機的朋友都知道,有時候在文本框中輸入文字后,操作按鈕被輸入法遮擋了,不得不關閉輸入法才可以繼續操作。
比如下面這個畫面:
畫面布局:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ll2"android:orientation="vertical"
- android:layout_width="fill_parent"android:layout_height="fill_parent">
- <CheckBoxandroid:id="@+id/widget57"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="@string/location"
- android:layout_gravity="right">
- </CheckBox>
- <EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:layout_weight="1"
- android:textColorHighlight="#cccccc"android:hint="你想說什么?"
- android:gravity="top"android:textSize="18sp">
- </EditText>
- <Buttonandroid:id="@+id/widget53"android:layout_width="100px"
- android:layout_height="wrap_content"android:text="@string/share"
- android:layout_gravity="right"/>
- </LinearLayout>
如果不做任何操作,那么點擊文本框后的效果肯定是下圖:
此時,【共享】按鈕被輸入法擋住了,必須關閉輸入法才可以操作了。
有的朋友會說,可以在布局外面再加一個ScrollView,這樣的畫,UI布局就和輸入法分離了,輸入法出現后,上面還可以滾動。
但是這樣的效果好嗎? 我們來看一下效果(布局外加ScrollView的效果):
畫面布局:
- <?xmlversion="1.0"encoding="utf-8"?>
- <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:id="@+id/ll2"android:orientation="vertical"
- android:layout_width="fill_parent"android:layout_height="fill_parent">
- <CheckBoxandroid:id="@+id/widget57"android:layout_width="wrap_content"
- android:layout_height="wrap_content"android:text="@string/location"
- android:layout_gravity="right">
- </CheckBox>
- <EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent"
- android:layout_height="400px"android:layout_weight="1"
- android:textColorHighlight="#cccccc"android:hint="你想說什么?"
- android:gravity="top"android:textSize="18sp">
- </EditText>
- <Buttonandroid:id="@+id/widget53"android:layout_width="100px"
- android:layout_height="wrap_content"android:text="@string/share"
- android:layout_gravity="right"/>
- </LinearLayout>
- </ScrollView>
從圖中可以看出,上面部分右側有一個滾動條,用戶可以通過從下滾動來點擊按鈕。但是個人覺得,這樣還不如直接關閉輸入法來點擊按鈕來的方便!
那么有沒有更好的辦法呢? 答案是有!
先看一下這個更好的方法是什么效果:
從圖中可以看出,UI布局也與輸入法分離了,同時EditText區域自動縮小了。這樣的話,即不影響用戶輸入,也不影響用戶進一步操作!
而且即使打開了輸入法,用戶也可以看到UI全貌,感覺比較舒服、友好。
下面就說一下,這個效果是如何做到的.
其實答案很簡單,不需要更改局部文件,而是修改AndroidManifest.xml文件,在Activity屬性中加一條:
- android:windowSoftInputMode="adjustResize"
AndroidManifest.xml修改前后比較:
該屬性還有一些其他值,大家可以上網查一下。 (這邊給個鏈接: http://blog.csdn.net/liluo1217/archive/2011/02/14/6184169.aspx)