Android App仿QQ制作Material Design风格沉浸式状态栏

2019-12-10 18:09:55于丽

res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="primary">#FF03A9F4</color>
  <color name="primary_dark">#FF0288D1</color>
  <color name="status_bar_color">@color/primary_dark</color>
</resources>

下面定义几个styles.xml

注意文件夹的路径:

values/styles.xml

<resources>
  <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">#FF4081</item>
  </style>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="@style/BaseAppTheme">
  </style>
</resources>

values-v19

<resources>

  <style name="AppTheme" parent="@style/BaseAppTheme">
    <item name="android:windowTranslucentStatus">true</item>
  </style>
</resources>

ok,这个没撒说的。注意我们的主题是基于NoActionBar的,Android:windowTranslucentStatus这个属性是v19开始引入的。

2、布局文件

activity_main.xml

<android.support.v4.widget.DrawerLayout
  xmlns:android="http://www.easck.com/apk/res/android"
  xmlns:app="http://www.easck.com/apk/res-auto"
  xmlns:tools="http://www.easck.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >


  <LinearLayout
    android:id="@+id/id_main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
      android:id="@+id/id_toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:fitsSystemWindows="true"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


    <TextView
      android:id="@+id/id_tv_content"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:gravity="center"
      android:text="HelloWorld"
      android:textSize="30sp"/>
  </LinearLayout>


  <android.support.design.widget.NavigationView
    android:id="@+id/id_nv_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header_just_username"
    app:menu="@menu/menu_drawer"
    />
</android.support.v4.widget.DrawerLayout>