防止UI界面被輸入法遮擋(畫面隨輸入法自適應)


應用過Android手機的朋友都知道,有時候在文本框中輸入文字后,操作按鈕被輸入法遮擋了,不得不關閉輸入法才可以繼續操作。

比如下面這個畫面:

 

畫面布局:

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@+id/ll2"android:orientation="vertical" 
  4.     android:layout_width="fill_parent"android:layout_height="fill_parent"> 
  5.     <CheckBoxandroid:id="@+id/widget57"android:layout_width="wrap_content" 
  6.         android:layout_height="wrap_content"android:text="@string/location" 
  7.         android:layout_gravity="right"> 
  8.     </CheckBox> 
  9.     <EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent" 
  10.         android:layout_height="fill_parent"android:layout_weight="1" 
  11.         android:textColorHighlight="#cccccc"android:hint="你想說什么?" 
  12.         android:gravity="top"android:textSize="18sp"> 
  13.     </EditText> 
  14.     <Buttonandroid:id="@+id/widget53"android:layout_width="100px" 
  15.             android:layout_height="wrap_content"android:text="@string/share" 
  16.             android:layout_gravity="right"/> 
  17. </LinearLayout> 

 

 

如果不做任何操作,那么點擊文本框后的效果肯定是下圖:

 

此時,【共享】按鈕被輸入法擋住了,必須關閉輸入法才可以操作了。

 

 

有的朋友會說,可以在布局外面再加一個ScrollView,這樣的畫,UI布局就和輸入法分離了,輸入法出現后,上面還可以滾動。

 

但是這樣的效果好嗎? 我們來看一下效果(布局外加ScrollView的效果):

 

畫面布局:

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent"android:layout_height="fill_parent" 
  4. android:orientation="vertical"> 
  5. <LinearLayout  
  6.     android:id="@+id/ll2"android:orientation="vertical" 
  7.     android:layout_width="fill_parent"android:layout_height="fill_parent"> 
  8.     <CheckBoxandroid:id="@+id/widget57"android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content"android:text="@string/location" 
  10.         android:layout_gravity="right"> 
  11.     </CheckBox> 
  12.     <EditTextandroid:id="@+id/widget34"android:layout_width="fill_parent" 
  13.         android:layout_height="400px"android:layout_weight="1" 
  14.         android:textColorHighlight="#cccccc"android:hint="你想說什么?" 
  15.         android:gravity="top"android:textSize="18sp"> 
  16.     </EditText> 
  17.     <Buttonandroid:id="@+id/widget53"android:layout_width="100px" 
  18.             android:layout_height="wrap_content"android:text="@string/share" 
  19.             android:layout_gravity="right"/> 
  20. </LinearLayout> 
  21. </ScrollView> 

 

 

從圖中可以看出,上面部分右側有一個滾動條,用戶可以通過從下滾動來點擊按鈕。但是個人覺得,這樣還不如直接關閉輸入法來點擊按鈕來的方便!

 

那么有沒有更好的辦法呢? 答案是有!

先看一下這個更好的方法是什么效果:

 

 

從圖中可以看出,UI布局也與輸入法分離了,同時EditText區域自動縮小了。這樣的話,即不影響用戶輸入,也不影響用戶進一步操作!

而且即使打開了輸入法,用戶也可以看到UI全貌,感覺比較舒服、友好。

 

下面就說一下,這個效果是如何做到的.

 

其實答案很簡單,不需要更改局部文件,而是修改AndroidManifest.xml文件,在Activity屬性中加一條:

  1. android:windowSoftInputMode="adjustResize" 

 

 

AndroidManifest.xml修改前后比較:

 

該屬性還有一些其他值,大家可以上網查一下。 (這邊給個鏈接: http://blog.csdn.net/liluo1217/archive/2011/02/14/6184169.aspx)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM