您的位置:首页 > 其它

底部EditText输入框遮挡问题分析与总结

2016-04-19 10:39 375 查看
最近在研究EditText输入框遮挡的问题。

底部输入框被软键盘遮挡的问题

这个问题的情景就是当我要输入的时候,EditText获取焦点,软件盘弹出来,把我的Edittext输入框挡住了。

1、这里我开始的解决方案:在清单文件中的Activity下,加入"adjustPan";

加入这个属性之后发现果然可以,但是新的问题出现了,那就是整体布局往上移,这个用户体验很不好。

<activity
android:name="cn.xs8.app.activity.news.Xs8_News_Login_RegNomal"
android:windowSoftInputMode="adjustPan" />
<activity


在Activity中设置:Android:windowSoftInputMode="stateUnspecified",默认设置:软键盘的状态(隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。

"stateUnchanged", 软键盘被保持上次的状态。

"stateHidden", 当用户选择该Activity时,软键盘被隐藏。

"stateAlwaysHidden", 软键盘总是被隐藏的。

"stateVisible",. 软键盘是可见的。

"stateAlwaysVisible", 当用户选择这个Activity时,软键盘是可见的。

"adjustResize", (压缩模式)当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。

"adjustPan"(平移模式:当输入框不会被遮挡时,该模式没有对布局进行调整,然而当输入框将要被遮挡时,窗口就会进行平移。也就是说,该模式始终是保持输入框为可见。(键盘遮挡使用这种方法就能解决了!)

2、在网上查了一下,找到一种沉浸式状态栏实现思路,可以解决这个问题

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:fitsSystemWindows="true"
tools:context="com.saidtx.myapplication.TestActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/edit"
android:background="@android:color/white">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="@string/previews"/>
</LinearLayout>
</ScrollView>

<LinearLayout
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

</RelativeLayout>

首先必须要有ScrollView去包裹内容,除了状态栏。

android:fitsSystemWindows="true"
android:background="@android:color/holo_green_light"

上面两行才是关键,“android:fitsSystemWindows =“true”” 在谷歌官网的解释是这样的:

android 布局xml中 android:fitsSystemWindows="true"的原文解析为:

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

内置的一个布尔值属性,通过其去调整基于系统窗口的视图布局,例如状态栏,如果该值为真,调整这个视图的内边距与系统窗口的距离,只有该view是non-embedded(非嵌入的)的activity才会产生影响。

即在开放过程中设计应用程序布局时当考虑当系统窗口的影响时,设置该值,如果为true,将自动调整系统窗口布局来适应你自定义的布局。例如:当系统有状态栏,你的应用也存在状态栏时便可以设置为ture。

关键上面两个属性,还有需要在其他子布局添加背景,不然就跟随了最外层的背景,代码部分还是采用网上通用方案,只是不需要自定义的状态栏了,也不需要计算状态栏的高度,也不需要在清单文件中添加属性了。



3,还有第三种方案解决,在代码中判断

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View statusBar = findViewById(R.id.status_bar);

setContentView(R.layout.activity_test);
//判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,不然低版本会多出来一个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
statusBar.setVisibility(View.VISIBLE);
//还有设置View的高度,因为每个型号的手机状态栏高度都不相同
}else{
statusBar.setVisibility(View.GONE);
}
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}

第三种方案也有自身的缺陷,这种方式适用于使用抽屉菜单或者滑动返回效果的时候,但是输入框还是会被遮挡。

参考:http://www.jianshu.com/p/b100b64544f3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: