Android—沉浸式狀態欄

我們的征程是星辰大海,而非人間煙塵
文章目錄
- Android---沉浸式狀態欄
- 去掉標題欄
- 效果
- 引入依賴
- 沉浸狀態欄顏色
- 沉浸狀態欄圖片
去掉標題欄
首先去掉對應主題下面的
Android自帶的ActionBar,只需要在對應主題下面加NoActionBar

效果

引入依賴
implementation 'com.jaeger.statusbarutil:library:1.5.1'
沉浸狀態欄顏色
沉浸式狀態欄,既可以把顏色實作沉浸,又可以把圖片實作沉浸,
未設定顏色沉浸的狀態:

邏輯代碼:
StatusBarUtil.setColor(MainActivity.this,getResources().getColor(R.color.teal_200),0)

布局檔案

沉浸狀態欄圖片
未沉浸之前的效果

沉浸之后的效果

布局代碼
- 將要沉浸的圖片放在ImageView組件里面
- 其他的組件放在一個布局里面
- 通過一個
setTransparentForImageView方法替換

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="250dp"
android:src="@drawable/w"
android:layout_margin="0dp"
android:padding="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="115dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
邏輯代碼

StatusBarUtil.setTransparentForImageView(this,findViewById(R.id.constraintLayout));
StatusBarUtil官網
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/351004.html
標籤:其他
