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

来源:互联网 发布:辛德勒的名单影评知乎 编辑:程序博客网 时间:2024/05/17 11:05

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

public class MainActivity extendsActivity {
 
    @Override
    protectedvoid 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
    android:background="#ffffff"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <textview
        android:background="#009959"
        android:layout_height="100dp"
        android:layout_width="match_parent">
        <button
            android:background="#ff669d/"
            android:layout_height="50dp"
            android:layout_width="100dp">
        </button>
    </textview>
</linearlayout>

但这样,其实还是有问题的,我在textView里面加一些文字,就是绿色的那一块,大家看一下效果:

\

文字和状态栏重叠在一起了,这肯定是不行的,此时需要添加下面的代码:

android:fitsSystemWindows=true
android:clipToPadding=true

现在布局文件是这样的:


<linearlayout
    android:background="#ffffff"
    android:cliptopadding="true"
    android:fitssystemwindows="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <textview
        android:background="#009959"
        android:layout_height="100dp"
        android:layout_width="match_parent">
        <button
            android:background="#ff669d/"
            android:layout_height="50dp"
            android:layout_width="100dp">
        </button>
    </textview>
</linearlayout>


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

\

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

<linearlayout
    android:background="#ffffff"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <textview
        android:background="#009959"
        android:cliptopadding="true"
        android:fitssystemwindows="true"
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:text="你好,请问你有男朋友吗/">
        <button
            android:background="#ff669d/"
            android:layout_height="50dp"
            android:layout_width="100dp">
        </button>
    </textview>
</linearlayout>

就是那两行红色的代码,放在绿色的textView上,这样,就会是下面的效果:

 

这就是我们想要的了。


0 0