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

给布局文件根控件设置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;
    }

你可能感兴趣的:(解决华为等型号虚拟按键影响布局问题)