android 状态栏与标题栏一体化

step1.设置 Acitivity 所在 window 的属性:

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

        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(
                            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

    }

step2.由于要实现的是状态栏和顶部的控件是同一个颜色,同时,控件内容也不和状态栏重复,所以只要把下面两行代码放到我们顶部的控件就可以了:

 android:fitsSystemWindows="true"
 android:clipToPadding="true"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:background="#32A082">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="#32A082">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="#fff"
            android:text="顶部"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"></LinearLayout>
</LinearLayout>

点击打开链接

发布了16 篇原创文章 · 获赞 2 · 访问量 2032

猜你喜欢

转载自blog.csdn.net/weixin_42127169/article/details/80641350