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("沉浸式");



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章