android design+ 透明状态栏

5.0 上设置主题



沉浸式状态栏:

在style-v19, style-v21 的theme 中设置

<item name="android:windowTranslucentStatus">true</item>

状态栏变成透明,并且状态栏的位置会被下面的内容占据,如果不想被占据可以设置

android:fitsSystemWindows="true"

fitSystemWindows属性: 
官方描述: 
Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity. 
简单描述: 
这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间…. 
实际效果: 
当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。


由于Android系统碎片化,可能在style中设置透明不管用,可以使用代码设置,但是必须在setConentView()前调用

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
       // activity.getWindow().setStatusBarColor(Color.TRANSPARENT);  //直接用这个方法会有兼容性问题
        Window window = activity.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)
    }
}

非沉浸式设置:

设置

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

在21及以上状态栏会变成设置的颜色,低于21的状态栏是黑色

全屏设置:

<item name="android:windowFullscreen">true</item>

设置全屏后,状态栏里电量时间等也看不到,而沉浸式是可以看到的


设置底部虚拟按键背景

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  2.             getWindow().setNavigationBarColor(Color.parseColor("#1bb5d7"));  
  3.             //getWindow().setNavigationBarColor(getResources().getColor(R.color.black));  
  4.             //getWindow().setNavigationBarColor(Color.BLUE);  
  5. }  

折叠式标题栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/ivImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:transitionName="transition_wechat_img"
                android:src="@drawable/cat"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"
                />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nestedscrollview_wechat"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/strDemo"
            android:background="#ff0000"
            />

    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>
如果要沉浸式状态栏可以设置状态栏透明,在activity中设置

toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("沉浸式");



猜你喜欢

转载自blog.csdn.net/xxzjwdnlwx/article/details/51482225