android UI隨輸入法自動調整(改變)

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

比如下面這個畫面:

 

畫面佈局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.     <CheckBox android: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.     <EditText android: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.     <Button android: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. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns: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.     <CheckBox android: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.     <EditText android: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.     <Button android: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)

發佈了52 篇原創文章 · 獲贊 178 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章