解决华为等型号虚拟按键影响布局问题

给布局文件根控件设置padding即可,以下是Kotlin代码:

    setContentView(R.layout.activity_home)
    homeLayout!!.setPadding(0, BaseActivity.getStatusHeight(context), 0, BaseActivity.getBottomStatusHeight(context))

BaseActivity尚未转换为Kotlin:

    /**
     * 获得状态栏的高度
     */
    public static int getStatusHeight(Context context) {

        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }
    /**
     * 获取 虚拟按键的高度
     */
    public static int getBottomStatusHeight(Context context) {
        int totalHeight = getDpi(context);

        int contentHeight = getScreenHeight(context);

        return totalHeight - contentHeight;
    }

    原文作者:Qin0821
    原文地址: https://www.jianshu.com/p/09d45d69090d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞