【Android】防止UI介面被IME遮擋(畫面隨IME自適應)

來源:互聯網
上載者:User

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

比如下面這個畫面:

畫面布局:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:id="@+id/ll2" android:orientation="vertical"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"<br />android:layout_height="wrap_content" android:text="@string/location"<br />android:layout_gravity="right"><br /></CheckBox><br /><EditText android:id="@+id/widget34" android:layout_width="fill_parent"<br />android:layout_height="fill_parent" android:layout_weight="1"<br />android:textColorHighlight="#cccccc" android:hint="你想說什嗎?"<br />android:gravity="top" android:textSize="18sp"><br /></EditText><br /><Button android:id="@+id/widget53" android:layout_width="100px"<br />android:layout_height="wrap_content" android:text="@string/share"<br />android:layout_gravity="right"/><br /></LinearLayout><br /> 

如果不做任何操作,那麼點擊文字框後的效果肯定是:

此時,【共用】按鈕被IME擋住了,必須關閉IME才可以操作了。

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

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

畫面布局:

<?xml version="1.0" encoding="utf-8"?><br /><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical"><br /><LinearLayout<br />android:id="@+id/ll2" android:orientation="vertical"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"<br />android:layout_height="wrap_content" android:text="@string/location"<br />android:layout_gravity="right"><br /></CheckBox><br /><EditText android:id="@+id/widget34" android:layout_width="fill_parent"<br />android:layout_height="400px" android:layout_weight="1"<br />android:textColorHighlight="#cccccc" android:hint="你想說什嗎?"<br />android:gravity="top" android:textSize="18sp"><br /></EditText><br /><Button android:id="@+id/widget53" android:layout_width="100px"<br />android:layout_height="wrap_content" android:text="@string/share"<br />android:layout_gravity="right"/><br /></LinearLayout><br /></ScrollView> 

可以看出,上面部分右側有一個捲軸,使用者可以通過從下滾動來點擊按鈕。但是個人覺得,這樣還不如直接關閉IME來點擊按鈕來的方便!

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

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

可以看出,UI布局也與IME分離了,同時EditText地區自動縮小了。這樣的話,即不影響使用者輸入,也不影響使用者進一步操作!

而且即使開啟了IME,使用者也可以看到UI全貌,感覺比較舒服、友好。

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

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

android:windowSoftInputMode="adjustResize" 

AndroidManifest.xml修改前後比較: 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.