Android学习笔记——解决设置了android:layout_alignParentBottom="true"的控件会被弹出的输入法顶上去的问题

在Android开发中,当输入法弹出时,使用android:layout_alignParentBottom="true"的控件会被顶上。通过在AndroidManifest.xml中设置adjustPan并未完全解决问题。解决方案是在代码中动态设置LinearLayout的marginTop,使其在输入法弹出时仍保持在屏幕底部。通过计算屏幕高度、底部导航条高度和控件高度的一半来确定marginTop的值,从而确保布局始终正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面:在项目开发当中有时会碰到许多奇奇怪怪的问题,这次是碰上了弹出输入法对界面造成影响的问题。

一、出现问题
先来看看出现问题的场景:
这里写图片描述

这里写图片描述

这个界面整体是由若干个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);
    }

这些方法在网上都能非常轻易地找到,有兴趣,想更深地了解里面具体参数的含义的童鞋可以自己去找找,我就不在这详细讲了。
最后贴一张解决了问题的截图进行收尾吧(:з」∠),如果各位有啥疑问或者意见,非常欢迎来指导和交流~~
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值