Android-沉浸式布局的玩法

  • 沉浸式(状态栏和虚拟按键透明)
    • 第一种(配置文件的方式)
    • 第二种(代码的方式)
  • 沉浸式(修改状态栏、虚拟按键的颜色)
    • 第一种(需要修改自己的布局文件)
    • 第二种(修改系统的根布局)

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

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

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

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

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">                "colorPrimary">@color/colorPrimary        "colorPrimaryDark">@color/colorPrimaryDark        "colorAccent">@color/colorAccent                "android:windowTranslucentStatus" tools:targetApi="kitkat">false                "android:windowTranslucentNavigation" tools:targetApi="kitkat">true                "android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent    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. Android高手进阶教程(六)之----Android(安卓)中MenuInflater的使
  2. Android(安卓)自定义button简单示例
  3. android之网格布局和线性布局实现注册页面
  4. 【Android(安卓)界面效果25】android中include标签的使用
  5. Android的布局管理器(上篇)-LinearLayout、TableLayout、FrameLayo
  6. Android性能优化-布局优化
  7. Android(安卓)初级面试者拾遗(前台界面篇)之 View 和 ViewGroup
  8. android中ImageView属性及其详解
  9. android View的getHeight和getMeasuredHeight的区别

随机推荐

  1. Android之--电话归属地的悬浮框的…
  2. 简单显示顶部通知的Demo
  3. Setting Ubuntu16.04 environment for an
  4. Android_通过传感器抓小偷
  5. android framework之Bluetooth
  6. android属性动画 —— ValueAnimator和Ob
  7. Android(安卓)解决资源id冲突
  8. android 7.0 手机android.content.res.Xm
  9. Android中Java与JavaScript之间交互
  10. 流式布局 ChipsLayoutManager