沉浸式开启后,底部虚拟导航栏遮挡页面问题

本文介绍了一种在Android应用中判断并适配虚拟导航栏的方法。通过自定义工具类,可以检测虚拟导航栏的存在,并调整应用布局,确保在不同设备上的一致体验。适用于华为5.0小屏及华为P20 9.0系统的异形屏。

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

在onCreatView判断,是否有虚拟按钮

if (ScreenUtils.isShowNavBar(this)) {
    getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, ScreenUtils.getNavigationBarHeight(this));
}

剩余调用下面工具类方法.

/**
 * 判断是否显示了导航栏 * (说明这里的context 一定要是activity的context 否则类型转换失败) * * @param context * @return
 */
public static boolean isShowNavBar(Context context) {
    if (null == context) {
        return false;
    }
    //获取应用区域高度
    Rect outRect1 = new Rect();
    try {
        ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
    } catch (ClassCastException e) {
        e.printStackTrace();
        return false;
    }
    //状态栏高度
    int activityHeight = outRect1.height();
    //屏幕物理高度 减去 状态栏高度
    int statusBarHeight = getStatusBarHeight();
    //剩余高度跟应用区域高度相等 说明导航栏没有显示 否则相反
    int remainHeight = getRealHeight() - statusBarHeight;
    if (activityHeight == remainHeight) {
        return false;
    } else {
        return true;
    }
}

/**
 * 获取状态栏高度 * * @return
 */
private static int getStatusBarHeight() {
    int result = 0;
    int resourceId = App.getInstance().getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = App.getInstance().getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

/**
 * 活动屏幕信息
 */
private static WindowManager wm;

/**
 * 获取真实屏幕高度 * * @return
 */
@SuppressLint("ObsoleteSdkInt")
private static int getRealHeight() {
    if (null == wm) {
        wm = (WindowManager) App.getInstance().getSystemService(Context.WINDOW_SERVICE);
    }
    Point point = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        wm.getDefaultDisplay().getRealSize(point);
    } else {
        wm.getDefaultDisplay().getSize(point);
    }
    return point.y;
}


/**
 * 获取底部导航栏高度
 */
public static int getNavigationBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    //获取NavigationBar的高度
    return resources.getDimensionPixelSize(resourceId);
}

总结:华为5.0小屏和华为p20 9.0系统异形屏都可以.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土狗的想法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值