隐藏状态栏和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(效果能达到一部分要求,但还是有黑影,不是完全的透明)
![](https://img.haomeiwen.com/i16287050/ee5258cc7cc74950.jpg)
下面是完全透明的效果
环境是 androidx
- 1、添加依赖(Material Design的依赖)
这一步自己按自己的实际应用来,我这里用到了 Material Design
implementation 'com.google.android.material:material:1.3.0-alpha01'
- 2、在 res 文件下的 values 文件的 styles.xml 文件中
<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 的两个文件夹
![](https://img.haomeiwen.com/i16287050/0569f3f2ffe3f63f.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 就行了
- 3、最后在需要使状态栏透明的界面的xml文件中
如果布局的内容会显示到状态栏,就在布局节点上加上以下一行代码
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>
![](https://img.haomeiwen.com/i16287050/97a22959c00159d5.jpg)
网友评论