美文网首页UI
Android刘海屏适配

Android刘海屏适配

作者: DINGCHAO_ | 来源:发表于2020-12-07 10:12 被阅读0次

    刘海屏幕的适配,在全面屏设置下,顶部有刘海的区域,有的会导致刘海把标题栏等内容遮盖如下图,这是美团的统一版本在小米和华为上的适配,可以看到小米的未遮挡内容,但在华为设备上,明显刘海遮住了一部分内容,目前手里只有这两台设备,所以没有更多机型测试,不过根据上线APP反馈,目前这个方法可以满足。此博文为笔记内容,所以内容不多


    lh1.jpg
    lh2.jpg

    如果统一留出固定的高度,会在不同设备上造成不同的效果,现在使用动态计算顶部状态栏高度然后预留高度,防止被异形屏遮挡情况,使用之后的效果如下图


    lh3.jpg
    lh4.jpg

    方法及使用

    setStatusBarHeight方法

    public static void setStatusBarHeight(Activity activity, View view) {
            int bar_height =getStatusBarHeight(activity);
            LinearLayout.LayoutParams layoutParams;
            layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.height = bar_height - 12;
            view.setLayoutParams(layoutParams);
        }
    
     public static int getStatusBarHeight(Context context) {
            int result = 0;
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = context.getResources().getDimensionPixelSize(resourceId);
            }
            return result;
        }
    
    

    使用方法,在布局上设置一个占位条,需要适配的引用进来,因为有些布局是不用设置的

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/ll_include_status_bar_height_dynamic"
        android:layout_height="wrap_content">
        <!--状态栏动态填充高度,只在会影响的时候才需要,
        比如标题栏的时候,计算高度填充防止标题栏被刘海屏幕遮挡-->
        <View
            android:id="@+id/v_include_status_bar_height_dynamic"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@color/color_00000000" />
    </LinearLayout>
    

    调用设置

     @BindView(R.id.v_include_status_bar_height_dynamic)
        View v_include_status_bar_height_dynamic;
    
     Tool.setStatusBarHeight(MainActivity.this, v_include_status_bar_height_dynamic);
    

    相关文章

      网友评论

        本文标题:Android刘海屏适配

        本文链接:https://www.haomeiwen.com/subject/wfezwktx.html