Android-沉浸式布局的玩法


这里来记录两种沉浸式布局的设置方法

沉浸式(状态栏和虚拟按键透明)

第一种(配置文件的方式)

修改我们的主题配置
注意区分Android版本
android:windowTranslucentStatus:状态栏透明设置 4.4以上的版本设置false 4.4以下的需要设置true;

<!--    NoActionBar  是去掉标题栏-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
<!--        沉浸式  布局   start   配置文件的方式-->
        <!-- 状态栏透明设置 4.4以上的版本设置false   4.4以下的需要设置true-->
        <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">false</item>
        <!-- 虚拟按键透明设置 -->
        <item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
        <!-- 5.0以上设置状态栏的颜色  但是必须是windowTranslucentStatus为false  -->
        <item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
    </style>

第二种(代码的方式)

相比上面的配置文件的方式,原理是一样,也是获取到android:windowTranslucentStatus和android:windowTranslucentNavigation这两个值,然后去根据版本修改状态;

/**
     * 代码的方式设置沉浸式布局
      */
    private void initStatus() {
        //版本大于等于4.4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //获取到状态栏设置的两条属性
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            //在4.4之后又有两种情况  第一种 4.4-5.0   第二种 5.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //第二种 5.0以上
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                window.setStatusBarColor(0);
            } else {
                //第一种 4.4-5.0
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }

沉浸式(修改状态栏、虚拟按键的颜色)

第一种(需要修改自己的布局文件)

这类我们在自己的布局文件中添加了一个占位用来代替状态栏的view控件;

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="观察者测试下"
        android:gravity="center"
        app:layout_behavior=".view.MyBehavior"></TextView>
    <com.lk.myrecyclerview.view.MyRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"/>
<!--    用来代替状态栏  第一种方式-->
    <View
        android:id="@+id/statusBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></View>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

注意这类需要先设置和上面一样需要先修改状态栏的透明;
在去获取我们写的代替状态栏的控件,然后获取到状态栏的高度,将我们控件设置成状态栏的高度,然后设置自己需要的颜色;

 private void initStatus() {
        //版本大于等于4.4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //获取到状态栏设置的两条属性
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            //在4.4之后又有两种情况  第一种 4.4-5.0   第二种 5.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //第二种 5.0以上
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                window.setStatusBarColor(0);
            } else {
                //第一种 4.4-5.0
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
//第一种改变状态栏的颜色------------start-----------------
        //获取到用来代替状态栏的自己写的控件
        View statusBar = findViewById(R.id.statusBar);
        //获取到这个控件的属性对象
        ViewGroup.LayoutParams layoutParams = statusBar.getLayoutParams();
        //给这个控件设置成状态栏的高度
        layoutParams.height = getStatusHeight();
        statusBar.setLayoutParams(layoutParams);
        statusBar.setBackgroundColor(Color.BLUE);
//第一种改变状态栏的颜色------------end-----------------
        
    }
 /**
     * 获取状态栏的高度
     * @return
     */
    public int getStatusHeight(){
        //获取系统状态栏的资源id
        int resourcesId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        //大于0  就标识拿到了id
        if(resourcesId>0){
            //返回状态栏高度
            return getResources().getDimensionPixelSize(resourcesId);
        }
        return 0;
    }

第二种(修改系统的根布局)

这类是根据根布局DecorView获取到里面的content这个布局,然后设置上面(状态栏)和下面(虚拟按键,这个虚拟按键,没有这个需求的,可以不加)内边距,这个边距是根据状态栏和虚拟按键的高度去设置的;
然后适配,5.0以上可以直接设置getWindow().setStatusBarColor();
4.4-5.0之间的,是获取到根布局DecorView,然后也是创建出了一个View,然后设置其高度,然后添加到了这个根布局;
注:发现没?和我们上面玩法差不多,也是用了一个View,然后设置成状态栏的高度一样,然后设置背景色,添加到了布局当中…

/**
     * 代码的方式设置沉浸式布局
      */
    private void initStatus() {
        //版本大于等于4.4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //获取到状态栏设置的两条属性
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            //在4.4之后又有两种情况  第一种 4.4-5.0   第二种 5.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //第二种 5.0以上
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                window.setStatusBarColor(0);
            } else {
                //第一种 4.4-5.0
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
//第二种改变状态栏的颜色-----------start-------  不让内容填充状态栏-->3.在代码中设置padding值并且设置一个控件来代替状态栏 -----
        //获取content这个FrameLayout
        View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
        //给content布局设置padding值
        //为什么要设置内边距?
        //因为如果不设置的话,我们的内容区域所在状态栏位置的会被盖住
        rootView.setPadding(0,getStatusHeight(),0,getNavigationBarHeight());
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            //第二种 5.0以上
            getWindow().setStatusBarColor(Color.RED);
        }else{
            //第一种 4.4-5.0
            //获取到根布局
            ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
            View statusBar = new View(this);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusHeight());
            statusBar.setBackgroundColor(Color.RED);
            statusBar.setLayoutParams(layoutParams);
            decorView.addView(statusBar);
        }
        
//第二种改变状态栏的颜色-----------end-------  不让内容填充状态栏-->3.在代码中设置padding值并且设置一个控件来代替状态栏  -----
    }
/**
     * 获取到底部虚拟按键的高度
     * @return
     */
    public int getNavigationBarHeight(){
        //获取到虚拟按键的资源ID
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        //如果获取到了
        if(resourceId>0){
            //就返回它的高度
            return getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    /**
     * 获取状态栏的高度
     * @return
     */
    public int getStatusHeight(){
        //获取系统状态栏的资源id
        int resourcesId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        //大于0  就标识拿到了id
        if(resourcesId>0){
            //返回状态栏高度
            return getResources().getDimensionPixelSize(resourcesId);
        }
        return 0;
    }

文章末,感谢大家翻阅到最后,工作中有需要的,赶紧用起来吧

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值