android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

这个特性是andorid4.4支持的,最少要api19才干够使用。以下介绍一下使用的方法,很得简单:


添加一个demo源代码:

https://github.com/ws123/StatusDemo


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }


}


        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

仅仅要增加这两行代码,就能够实现沉浸式通知栏了。效果如图:



给大家看看这个界面的布局:

<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="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView

        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959" />


    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d"/>

</LinearLayout>

是一个垂直的流布局,但这样,事实上还是有问题的。我在textView里面加一些文字。就是绿色的那一块,大家看一下效果:


大家看到了吧,文字和状态栏重叠在一起了,这肯定是不行的,此时须要加入以下的代码:


    android:fitsSystemWindows="true"
    android:clipToPadding="true"

<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:fitsSystemWindows="true"
    android:clipToPadding="true"

    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959" />


    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d"/>

</LinearLayout>

大家看红色的那部分。增加那两行以后。界面仍然会是沉浸式的,但状态栏那部分,就不会再重叠了,像加了padding一样,例如以下图:


大家看图。绿色的textView和红色的一个button都被下移了。状态栏是白色的。是背景linearLayout的颜色。非常明显,这也不是我们想要的,我们希望状态栏和我们放在顶部的控件是同一个颜色,同一时候。控件内容也不和状态栏反复,事实上,仅仅要把那两行代码放到我们顶部的控件就能够了。代码例如以下:


<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="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">



    <TextView
        android:fitsSystemWindows="true"
        android:clipToPadding="true"

        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#009959"
        android:text="你好。请问你有男朋友吗"/>


    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ff669d"/>

</LinearLayout>
就是那两行红色的代码,放在绿色的textView上。这样。就会是以下的效果:

这就是我们想要的了。




原文地址:https://www.cnblogs.com/wzzkaifa/p/6727243.html