Android学习笔记——解决设置了android:layout_alignParentBottom="true"的控件会被弹出的输入法顶上去的问题
2016-11-01 23:06
811 查看
写在前面:在项目开发当中有时会碰到许多奇奇怪怪的问题,这次是碰上了弹出输入法对界面造成影响的问题。
一、出现问题
先来看看出现问题的场景:
这个界面整体是由若干个EditText输入框和两个置于底端按钮组成。我之前已在AndroidManifest.xml 配置文件中设置了Activity的Window属性adjustPan(更多关于Window属性的知识可以看下这篇博客:http://blog.csdn.net/twoicewoo/article/details/7384398),这解决了EditText下方的控件被弹出的输入法顶上去造成界面压缩的问题。但是,还有一个问题仍没有解决,那就是当我让EditText获得焦点时,处于屏幕底端的两个按钮会被输入法顶上去的问题。就像这样:
我的底部按钮布局是这样写的:
可以看到,我为底部镶嵌了两个Button控件的LinearLayout 设置了android:layout_alignParentBottom=”true” 这个属性,使它可以悬浮在ScrollView 上并一直处于屏幕底端。在尝试了多种设置配置文件和控件属性的方法都无法解决这个问题后,我想到了在代码中去动态设置LinearLayout 组件的marginTop 值的方法。
二、解决问题
解决的思路是这样,要让这个LinearLayout 组件(最外层是RelativeLayout布局)一直处于屏幕底端,且不被输入法顶上去,那就让这个组件和屏幕顶端保持一个不变的距离就好了。也就是保持android:layout_marginTop 的值不变。由于考虑到Android手机有多种分辨率,我们要去代码中进行动态设置这个值。具体这个值的算法是这样的:屏幕的高度 减去底部导航条的高度(如果有虚拟键) 再减去我们底部LinearLayout 组件一半的高度,最后得到的值就是我们需要的。
有了思路,解决问题就简单,网上能找到许多关于动态设置margin 值的资料,我在这里就不赘述了。直接上代码:
其中ScreenUtils.getScreenH(this)是获取屏幕高度的,ScreenUtils.getNavigationBarHeight(this) 是获取底部导航条高度的,单位都是px:
DpOrPx.dip2px(this,25) 是将LinearLayout 组件高度50dp的一半转成px单位:
这些方法在网上都能非常轻易地找到,有兴趣,想更深地了解里面具体参数的含义的童鞋可以自己去找找,我就不在这详细讲了。
最后贴一张解决了问题的截图进行收尾吧(:з」∠),如果各位有啥疑问或者意见,非常欢迎来指导和交流~~
一、出现问题
先来看看出现问题的场景:
这个界面整体是由若干个EditText输入框和两个置于底端按钮组成。我之前已在AndroidManifest.xml 配置文件中设置了Activity的Window属性adjustPan(更多关于Window属性的知识可以看下这篇博客:http://blog.csdn.net/twoicewoo/article/details/7384398),这解决了EditText下方的控件被弹出的输入法顶上去造成界面压缩的问题。但是,还有一个问题仍没有解决,那就是当我让EditText获得焦点时,处于屏幕底端的两个按钮会被输入法顶上去的问题。就像这样:
我的底部按钮布局是这样写的:
<LinearLayout android:id="@+id/divBottom" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@color/back_transparent" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_height="35dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_marginRight="15dp" android:background="@color/orange" android:text="保存模板" android:textSize="16dp" android:textColor="@color/white"/> <Button android:layout_width="match_parent" android:layout_height="35dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_weight="1" android:layout_marginLeft="15dp" android:layout_marginRight="20dp" android:background="@color/orange" android:text="预览" android:textSize="16dp" android:textColor="@color/white"/> </LinearLayout>
可以看到,我为底部镶嵌了两个Button控件的LinearLayout 设置了android:layout_alignParentBottom=”true” 这个属性,使它可以悬浮在ScrollView 上并一直处于屏幕底端。在尝试了多种设置配置文件和控件属性的方法都无法解决这个问题后,我想到了在代码中去动态设置LinearLayout 组件的marginTop 值的方法。
二、解决问题
解决的思路是这样,要让这个LinearLayout 组件(最外层是RelativeLayout布局)一直处于屏幕底端,且不被输入法顶上去,那就让这个组件和屏幕顶端保持一个不变的距离就好了。也就是保持android:layout_marginTop 的值不变。由于考虑到Android手机有多种分辨率,我们要去代码中进行动态设置这个值。具体这个值的算法是这样的:屏幕的高度 减去底部导航条的高度(如果有虚拟键) 再减去我们底部LinearLayout 组件一半的高度,最后得到的值就是我们需要的。
有了思路,解决问题就简单,网上能找到许多关于动态设置margin 值的资料,我在这里就不赘述了。直接上代码:
divBottom = (LinearLayout) findViewById(R.id.divBottom); RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) divBottom.getLayoutParams(); lp.setMargins(0, ScreenUtils.getScreenH(this)-ScreenUtils.getNavigationBarHeight(this)-DpOrPx.dip2px(this,25),0,0);
其中ScreenUtils.getScreenH(this)是获取屏幕高度的,ScreenUtils.getNavigationBarHeight(this) 是获取底部导航条高度的,单位都是px:
public static int getScreenH(Context context){ if (screenH == 0){ initScreen(context); } return screenH; } public static int getNavigationBarHeight(Context context) { Resources resources = context.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height","dimen" 4000 , "android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Navi height:" + height); return height; } private static void initScreen(Context context){ DisplayMetrics metric = context.getResources().getDisplayMetrics(); screenW = metric.widthPixels; screenH = metric.heightPixels; screenDensity = metric.density; }
DpOrPx.dip2px(this,25) 是将LinearLayout 组件高度50dp的一半转成px单位:
public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }
这些方法在网上都能非常轻易地找到,有兴趣,想更深地了解里面具体参数的含义的童鞋可以自己去找找,我就不在这详细讲了。
最后贴一张解决了问题的截图进行收尾吧(:з」∠),如果各位有啥疑问或者意见,非常欢迎来指导和交流~~
相关文章推荐
- android Relativelayout 某控件设置layout_alignParentRight=true,整行不显示问题
- listView 中relativeLayout 布局的 android:layout_alignParentBottom="true" 无效的解决办法
- listView 中relativeLayout 布局的 android:layout_alignParentBottom="true" 无效的解决办法
- 设置控件的透明度 ,android:layout_centerInParent="true" 失效
- RelativeLayout加上android:layout_alignParentBottom="true"让某一部分靠最底部
- include进来的控件无法用layout_alignParentBottom="true"
- 关于Linearlayout中控件设置为其底部的问题,android:layout_gravity="bottom"没效果
- android:layout_alignParentLeft="true"一下是什么意思
- Android中RelativeLayout各个属性 android:layout_alignParentLeft="true"找不到有时候
- Android输入法弹出是控件被顶上去问题
- Android之解决输入法软键盘弹出挤压屏幕或覆盖控件问题
- Android 技术总结(004)—— ScrollView中的组件设置android:layout_height="fill_parent"不起作用的解决办法
- Android ScrollView中的组件设置android:layout_height="fill_parent"不起作用的解决办法
- Android之解决输入法软键盘弹出挤压屏幕或覆盖控件问题
- listview的item为RelativeLayout时android:layout_alignParentBottom无效的解决方法
- RelativeLayout中子控件的android:layout_centerVertical="true"失效问题分析
- ScrollView里面嵌套子控件时设置 android:layout_width="match_parent"无效
- RelativityLayout 子控件的几个常用的属性 android:layout_centerHorizontal="true"、 android:layout_centerVertical="true" 、android:layout_centerInParent="true"
- 关于 android:layout_gravity="bottom" 或者 android:layout_gravity="right" 在LinearLayout失效问题
- android中scrollview内部组件android:layout_height="fill_parent"无效的解决办法