Android's ToolBar and custom ToolBar implement immersive status bar

The immersive status bar should be called a transparent status bar to be exact. Under normal circumstances, the background color of the status bar is black, and the immersive status bar is to set the status bar to be transparent or translucent.

The immersive status bar has appeared since android Kitkat (Android 4.4), and it can be set to the same color as the top of the APP, which makes the entire interface seem to switch to the same style as the APP when switching apps. It will appear more beautiful in the display of content.

This blog mainly talks about the two implementations of the status bar combined with ToolBar. The effect is as shown in the figure:

The prerequisite is that the Api is greater than or equal to 19 (version 4.4 and above)

Way 1:

layout toolbar1.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        app:title="App Title"
        app:subtitle="Sub Title"
        app:navigationIcon="@android:drawable/ic_input_add"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Immersive status bar"
            android:textSize="30sp" />
    </RelativeLayout>
</LinearLayout>

 

 

Activity.Java

 

public class TooBarStatusActivity1 extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//Go to the title
        setContentView(R.layout.toolbar_layout);
        //transparent status bar
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //transparent navigation bar
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //transparent navigation bar
        Toolbar mToolbar = (Toolbar) findViewById (R.id.toolbar);
        mToolbar.setTitle("App Title"); //Set the Toolbar title
        mToolbar.setSubtitle("Sub Title"); //Set Toolbar subtitle
        mToolbar.setLogo(R.mipmap.ic_launcher);//Set the Logo of Toolbar
        mToolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        setSupportActionBar (mToolbar);
    }
}

 

Way 2:

layout toolbar2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
   >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:fitsSystemWindows="true"
        android:clipToPadding="true"
        android:text="Custom ToolBar layout"
        android:textSize="20sp"
        android:gravity="center_vertical"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:text="Main layout"
        android:textSize="22sp"
        android:gravity="center"
        />
</LinearLayout>


Activity.Java

public class TooBarStatusActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.toolbar_layout2);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //transparent status bar
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //transparent navigation bar
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}


style.xml

 

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

 

 

 

Source code click to download

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326232097&siteId=291194637