隐藏状态栏和Toolbar+沉浸式状态栏

2019-05-04  本文已影响0人  沈溺_16e5

隐藏状态栏和Toolbar

在res文件夹下的values文件夹里的styles.xml文件中

<style name="FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
    <!--全屏的界面-->
    <item name="android:windowFullscreen">true</item>
</style>

在AndroidManifest.xml,给需要的Activity应用上面的style即可

<activity
    android:name=".ui.login.LoginActivity"
    android:theme="@style/FullScreen"/>

沉浸式状态栏

https://github.com/laobie/StatusBarUtil(效果能达到一部分要求,但还是有黑影,不是完全的透明)

半透明.jpg
下面是完全透明的效果
环境是 androidx
implementation 'com.google.android.material:material:1.3.0-alpha01'
<resources>

    <!--    需要添加com.google.android.material -->
    <!--    很多 Material Design UI 要求必须使用 MaterialComponents 主题 -->
    <style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textAllCaps">false</item>
    </style>

    <style name="TranslucentTheme" parent="MaterialAppTheme">
        <item name="windowNoTitle">true</item>
    </style>

</resources>

并且在 res 文件夹下创建文件夹名为 values-v19、values-v21 的两个文件夹


values.jpg

values-v19 文件夹下的 styles.xml 文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="TranslucentTheme" parent="MaterialAppTheme">
        <!-- Customize your theme here. -->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

values-v21 文件夹下的 styles.xml 文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="TranslucentTheme" parent="MaterialAppTheme">
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

在 AndroidManifest.xml 文件中给需要的 Activity 添加 theme 引用上面的 TranslucentTheme 就行了

android:fitsSystemWindows="true"

例如下面的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.CloudMusicActivity"
    android:id="@+id/drawer_layout">

    <!--主界面-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/color_cd3e3a"
        android:clickable="true"
        android:orientation="vertical">

        ......

    </LinearLayout>

    <!--从左侧划出来的菜单-->
    <LinearLayout
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        ......

    </LinearLayout>

    <!--从右侧划出来的菜单-->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>
完全透明.jpg
上一篇 下一篇

猜你喜欢

热点阅读