【Android】防止UI界面被输入法遮挡(画面随输入法自适应)

相应用过Android手机的朋友都知道,有时候在文本框中输入文字后,操作按钮被输入法遮挡了,不得不关闭输入法才可以继续操作。

比如下面这个画面:

画面布局:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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"> <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/location" android:layout_gravity="right"> </CheckBox> <EditText android: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> <Button android:id="@+id/widget53" android:layout_width="100px" android:layout_height="wrap_content" android:text="@string/share" android:layout_gravity="right"/> </LinearLayout>

如果不做任何操作,那么点击文本框后的效果肯定是下图:

此时,【共享】按钮被输入法挡住了,必须关闭输入法才可以操作了。

有的朋友会说,可以在布局外面再加一个ScrollView,这样的画,UI布局就和输入法分离了,输入法出现后,上面还可以滚动。

但是这样的效果好吗? 我们来看一下效果(布局外加ScrollView的效果):

画面布局:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns: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"> <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/location" android:layout_gravity="right"> </CheckBox> <EditText android: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> <Button android: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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值