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

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

DrawerLayout内部一个LinearLayout作为内容区域,一个NavigationView作为菜单。 
注意下Toolbar的高度设置为wrap_content。

然后我们的NavigationView中又依赖一个布局文件和一个menu的文件。

header_just_username.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://www.easck.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:background="?attr/colorPrimaryDark"
        android:orientation="vertical"
        android:padding="16dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

  <TextView
    android:id="@+id/id_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="16dp"
    android:text="http://www.easck.com/lmj623565791"/>

  <TextView
    android:id="@+id/id_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/id_link"
    android:text="Zhang Hongyang"/>

  <ImageView
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:layout_above="@id/id_username"
    android:layout_marginBottom="16dp"
    android:src="@mipmap/ic_launcher"/>


</RelativeLayout>

menu的文件就不贴了,更加详细的可以去参考Android 自己实现 NavigationView [Design Support Library(1)]。

大体看完布局文件以后,有几个点要特别注意:

(1)ToolBar高度设置为wrap_content
(2)ToolBar添加属性android:fitsSystemWindows="true"
(3)header_just_username.xml的跟布局RelativeLayout,添加属性android:fitsSystemWindows="true"
(4)android:fitsSystemWindows这个属性,主要是通过调整当前设置这个属性的view的padding去为我们的status_bar留下空间。

根据上面的解释,如果你不写,那么状态栏和Toolbar就会有挤一块的感觉了,类似会这样: