概述
写在前面:在项目开发当中有时会碰到许多奇奇怪怪的问题,这次是碰上了弹出输入法对界面造成影响的问题。
一、出现问题
先来看看出现问题的场景:
这个界面整体是由若干个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", "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学习笔记——解决设置了android:layout_alignParentBottom="true"的控件会被弹出的输入法顶上去的问题的全部内容,希望文章能够帮你解决Android学习笔记——解决设置了android:layout_alignParentBottom="true"的控件会被弹出的输入法顶上去的问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复