透明状态栏

适用于继承自AppCompatActivity并且没有设置过flag的Activity

上代码

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
这个属性4.4算是全透明(有的机子是过渡形式的透明),5.0就是半透明了 我的模拟器、真机都是半透明,

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){//4.4 全透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
}
以上代码基本解决适配各种版本全透明状态栏(如导航栏有需求可以再加导航栏)
场景一:状态栏和导航栏 背景是纯色
  • 把状态栏设置透明,然后在外面自己加一层View,设置背景
private static void setStatusBarBackgroundColor(View root, int color, boolean fromActivity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (root != null && root instanceof LinearLayout) {
                Activity context = (Activity) root.getContext();
                Window window = context.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.setStatusBarColor(color);
                    int systemUIVisibility = window.getDecorView().getSystemUiVisibility();
                    boolean isLightStatusBar = color == SignalColorHolder.YELLOW_COLOR;
                    if (isLightStatusBar) {
                        systemUIVisibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    } else {
                        systemUIVisibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    }
                    systemUIVisibility &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                    systemUIVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                    window.getDecorView().setSystemUiVisibility(systemUIVisibility);
                }

                if ((!fromActivity && Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    FrameLayout container = null;
                    if (((LinearLayout) root).getChildCount() > 0 && ((LinearLayout) root).getChildAt(0).getId() == R.id.gmf_status_bar_layer) {
                        container = (FrameLayout) ((LinearLayout) root).getChildAt(0);
                    }

                    final int statusBarHeight = getStatusBarHeight(root);

                    if (container != null) {
                        container.setBackgroundColor(color);
                    } else {
                        container = new FrameLayout(context);
                        container.setId(R.id.gmf_status_bar_layer);
                        container.setBackgroundColor(color);

                        {
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, statusBarHeight);
                            ((LinearLayout) root).addView(container, 0, params);
                        }

                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                            FrameLayout darkLayerVIew = new FrameLayout(context);
                            darkLayerVIew.setBackgroundResource(R.color.gmf_dark_layer);
                            {
                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, statusBarHeight);
                                container.addView(darkLayerVIew, params);
                            }
                        }
                    }
                }
            }
        }
    }
场景二:状态栏和导航栏背景透明 底部增加图片背景延伸到状态栏底部
  • 方法一:在Toolbar外面包一层父容器,在父容器上设置属性 fitsSystemWindows = true
  • 方法二:在Toolbar外面包一层父容器,在Toolbar顶部添加Space 高度 = 状态栏的高度
    注意:建议使用CoordinatorLayout,另外内容布局需要设置MarginTop 边距高度 = 状态栏高度 + 导航栏高度

适配系统高度

    <LinearLayout
        android:background="@android:color/transparent"
        android:id="@+id/section_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            style="@style/CustomizeToolbar"
            android:background="@android:color/transparent">

            <TextView
                style="@style/CustomizeToolbarTitle"
                android:layout_gravity="center"
                android:text="我的" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="right">

                <ImageButton
                    android:id="@+id/menu_more"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    android:src="@mipmap/ic_menu_more_white" />

            LinearLayout>

        android.support.v7.widget.Toolbar>

    LinearLayout>

布局写死高度:

<LinearLayout
        android:background="@android:color/transparent"
        android:id="@+id/section_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Space
            android:layout_width="match_parent"
            android:layout_height="25dp" />

        <android.support.v7.widget.Toolbar
            style="@style/CustomizeToolbar"
            android:background="@android:color/transparent">

            <TextView
                style="@style/CustomizeToolbarTitle"
                android:layout_gravity="center"
                android:text="我的" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="right">

                <ImageButton
                    android:id="@+id/menu_more"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    android:src="@mipmap/ic_menu_more_white" />

            LinearLayout>

        android.support.v7.widget.Toolbar>

    LinearLayout>

内容布局设置Margin 距离顶部高度为状态栏高度+导航栏高度

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp">

    FrameLayout>

你可能感兴趣的:(Android)