Android,沉浸式状态栏,状态栏以及Toolbar颜色分开设置

这里的状态栏和Toolbar 颜色是分开的,按照这几个步骤就可以实现 ,请看代码 :

   首先 :   

/**
 *     设置状态栏颜色
 */
public class StatusBarUtils {

    private static final int INVALID_VAL = -1;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static View compat(Activity activity, int statusColor) {
        int color = ContextCompat.getColor(activity, R.color.colorPrimaryDark);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (statusColor != INVALID_VAL) {
                color = statusColor;
            }
            activity.getWindow().setStatusBarColor(color);
            return null;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
            if (statusColor != INVALID_VAL) {
                color = statusColor;
            }
            View statusBarView = contentView.getChildAt(0);
            if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {
                statusBarView.setBackgroundColor(color);
                return statusBarView;
            }
            statusBarView = new View(activity);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(activity));
            statusBarView.setBackgroundColor(color);
            contentView.addView(statusBarView, lp);
            return statusBarView;
        }
        return null;

    }

    public static void compat(Activity activity) {
        compat(activity, INVALID_VAL);
    }


    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"?>
    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"
    android:orientation="vertical"
    android:fitsSystemWindows="true"   //  最好加上这句话 -->所有组件都要在屏幕范围之内
    tools:context="ce.mobsmstest.TestStatusActivity">

    
    layout="@layout/toolbar"/>

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#ff3"
        android:text="测试沉浸式状态栏"
        android:textColor="@android:color/black"
        android:textSize="20sp"/>

toolbar布局  :

xml version="1.0" encoding="utf-8"?>
    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:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    

            android:id="@+id/toolbar_title"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="21sp"
        tools:text="中间"/>

            android:id="@+id/toolbar_menu_title"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:paddingRight="15dp"
        android:textSize="16sp"
        tools:text="右面"/>

 

MainActivity 代码设置 :

  
public class TestStatusActivity extends AppCompatActivity {

    protected int statusBarColor = 0;
    protected View statusBarView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_status);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        // ----->设置 Toolbar颜色为绿色,所有设置要在 setSupportActionBar()之前
        toolbar.setBackgroundResource(R.color.lv_se);
        setSupportActionBar(toolbar);

        if (statusBarColor == 0) {
            //----> 设置状态栏颜色为黑色
            statusBarView = StatusBarUtils.compat(this, ContextCompat.getColor(this, R.color.black));
        } else if (statusBarColor != -1) {
            statusBarView = StatusBarUtils.compat(this, statusBarColor);
        }
        initStatus();
    }

    protected void initStatus() {
         // 判断当前系统是不是大于19, 4.4版本以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
             //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

}

整体效果  :

    状态栏  : ---->黑色

    Toolbar : ------> 绿色

Android,沉浸式状态栏,状态栏以及Toolbar颜色分开设置_第1张图片


额外补充知识点 : 动态设置高度

1. 在代码里面设置不要状态栏

this.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无ActionBar

2. 使用类似布局 :

xml version="1.0" encoding="utf-8"?>
    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:id="@+id/activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ggxiaozhi.store.the_basket.mvp.view.activity.HomeActivity">

            android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black_alpha_5"
        android:orientation="vertical">

                    android:id="@+id/status_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"/>

                    android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="46dp"
            app:tabIndicatorColor="@color/tab_selected_line_def"
            app:tabSelectedTextColor="@color/tab_text_selected_def"
            app:tabTextAppearance="@style/CustomTabLayoutTextAppearance"
            app:tabTextColor="@color/tab_text_normal_def"/>

        
        

    

            android:id="@+id/main_viewPage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


3. 通过反射获取状态栏的高度

/**
 * 获取状态栏的高度
 * @return  状态栏的高度
 */
protected int getStatusBarHeight(){
    try {
        //通过反射获取到类
        Class aClass=Class.forName("com.android.internal.R$dimen");
        //创建对象
        Object o=aClass.newInstance();
        //拿取属性
        Field status_bar_height = aClass.getField("status_bar_height");
        //获取值
        Object o1=status_bar_height.get(o);
        int height=Integer.parseInt(o1.toString());
        return getResources().getDimensionPixelOffset(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

4. 把状态栏的高度设置给布局里面的 linearlayout

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    final int statusHeight = getStatusBarHeight();
    status_bar.post(new Runnable() {
        @Override
        public void run() {
            android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) status_bar.getLayoutParams();
            params.height = statusHeight;
            status_bar.setLayoutParams(params);
        }
    });
}



再介绍一种实现方式 :

Qmui  -->  腾讯团队开发的UI框架,具体使用,自行百度,里面有沉浸式状态栏,具体实现,请看Demo

你可能感兴趣的:(Android)