├── .gitignore
├── .idea
├── .gitignore
├── compiler.xml
├── gradle.xml
├── kotlinc.xml
└── misc.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── demo
│ │ └── maple
│ │ └── fitsystembardemo
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── demo
│ │ │ └── maple
│ │ │ └── fitsystembardemo
│ │ │ └── MainActivity.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── values-night-v27
│ │ └── themes.xml
│ │ ├── values-night
│ │ └── themes.xml
│ │ ├── values-v27
│ │ └── themes.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ │ └── xml
│ │ ├── backup_rules.xml
│ │ └── data_extraction_rules.xml
│ └── test
│ └── java
│ └── demo
│ └── maple
│ └── fitsystembardemo
│ └── ExampleUnitTest.kt
├── build.gradle
├── fitSystemBar
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── xiaobin
│ │ └── fitsystembar
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── xiaobin
│ │ │ └── fitsystembar
│ │ │ ├── FitSystemBarHelper.kt
│ │ │ ├── SystemBarExtensions.kt
│ │ │ ├── base
│ │ │ ├── FitSystemBarConstraintLayout.kt
│ │ │ ├── FitSystemBarFrameLayout.kt
│ │ │ └── FitSystemBarLinearLayout.kt
│ │ │ ├── custom
│ │ │ ├── FitNavigationBarFrameLayout.kt
│ │ │ ├── FitNavigationBarLinearLayout.kt
│ │ │ └── FitStatusBarFrameLayout.kt
│ │ │ └── listener
│ │ │ └── OnLayoutRectChangeListener.kt
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ └── ids.xml
│ └── test
│ └── java
│ └── com
│ └── xiaobin
│ └── fitsystembar
│ └── ExampleUnitTest.kt
├── gif
└── screen.gif
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
20 |
21 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FitSystemBarUtils
2 |
3 | 沉浸式状态栏,一个自动处理WindowInsets和cutOut的工具,一行代码 给任意view绑定。
4 | 你还在苦恼布局中背景图需要上到状态栏或者虚拟导航键下面,却写不好其他控件的布局吗?
5 | 你还在获取状态栏高度的方式去给布局设置一个paddingTop吗?
6 | 快来试试看这个吧。
7 | 功能非常简单,没几行代码。
8 |
9 | 可以实现软键盘弹起时,平滑的推起底部的输入框哦!(也许比微信还丝滑)
10 |
11 | ## Demo录屏gif:
12 |
13 |
14 | ## 在项目中引用
15 |
16 | ### Gradle
17 |
18 | Step 1. Add it in your root build.gradle at the end of repositories
19 |
20 | allprojects {
21 | repositories {
22 | ...
23 | maven { url 'https://jitpack.io' }
24 | }
25 | }
26 |
27 | Step 2. Add the dependency
28 |
29 | [](https://jitpack.io/#Matcha-xiaobin/fitSystemBarUtils)
30 |
31 | dependencies {
32 | implementation 'com.github.Matcha-xiaobin:fitSystemBarUtils:1.0.3'
33 | }
34 |
35 | ### 目前BUG:
36 | 突然发现在Android 10系统上出现了系统栏明明隐藏了,高度却没有变成0的问题,android 11+似乎没问题。
37 | 目前已修复,并且禁用了低于android 11的动画效果,实际上ViewCompat中帮我们实现了低于android 11的设备的动画效果,但是存在问题。
38 |
39 | ### 在代码中使用:
40 | 这个工具本质是把系统分发的insets属性拿出来用了一下,可以参考下google的 BottomNavigationView 底部导航 的代码,里面也用到了这个原理。
41 | 注意:如果你不知道这个时干啥用的,建议下载demo看看实际效果是不是你需要的效果
42 |
43 | 任意View实现FitSystemBar效果
44 | 中间内容区域上下两边不需要填充,因为上面有标题栏,下面有输入框栏
45 |
46 | FitSystemBarHelper.attachView(
47 | binding.content,
48 | start = true,
49 | top = false,
50 | end = true,
51 | bottom = false
52 | )
53 | //第二种写法, 这种写法支持动态改变,但改变后的生效时间由系统分发WindowInsets的时机决定
54 | //如果需要立即生效,请在改变值后调用 fitSystemHelperForContent.requestApplyInsets()
55 | FitSystemBarHelper.attachView(binding.content) { orientation ->
56 | when (orientation) {
57 | FitSystemBarHelper.Orientation.Start -> true
58 | FitSystemBarHelper.Orientation.Top -> false
59 | FitSystemBarHelper.Orientation.End -> true
60 | FitSystemBarHelper.Orientation.Bottom -> false
61 | }
62 | }
63 |
64 | 获取这个View已绑定的FitSystemBarHelper对象
65 |
66 | view.fitSystemBarHelper()
67 |
68 | 取消绑定
69 |
70 | view.fitSystemBarHelper()?.destory()
71 |
72 | ### 使用DataBinding在xml中使用
73 | 每个属性的默认值为false
74 | 是否处理Start方向的
75 |
76 | fitStart="@{true}"
77 |
78 | 是否处理上方的
79 |
80 | fitTop="@{true}"
81 |
82 | 是否处理End方向的
83 |
84 | fitEnd="@{true}"
85 |
86 | 是否处理下方的
87 |
88 | fitBottom="@{true}"
89 |
90 | ### 自定义View
91 | 自带了3个:
92 | FitNavigationBarFrameLayout 用作根布局最底下的不需要处理顶部状态栏区域的填充的 FrameLayout
93 | FitNavigationBarLinearLayout 用作根布局最底下的不需要处理顶部状态栏区域的填充的 LinearLayout
94 | FitStatusBarLinearLayout 用作根布局最上方的不需要处理底部导航栏区域的填充的 LinearLayout
95 | 直接参考即可,甚至可以直接拷贝一模一样,将继承的目标换成你需要的View即可
96 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | id "kotlin-kapt"
5 | }
6 |
7 | android {
8 | namespace 'demo.maple.fitsystembardemo'
9 | compileSdk 34
10 |
11 | defaultConfig {
12 | applicationId "demo.maple.fitsystembardemo"
13 | minSdk 21
14 | targetSdk 34
15 | versionCode 1
16 | versionName "1.0"
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 | compileOptions {
28 | sourceCompatibility JavaVersion.VERSION_17
29 | targetCompatibility JavaVersion.VERSION_17
30 | }
31 | kotlinOptions {
32 | jvmTarget = '17'
33 | }
34 | buildFeatures {
35 | dataBinding = true
36 | }
37 | }
38 |
39 | dependencies {
40 | implementation project(":fitSystemBar")
41 | implementation 'androidx.core:core-ktx:1.12.0'
42 | implementation 'androidx.activity:activity-ktx:1.8.0-beta01'
43 | implementation 'androidx.appcompat:appcompat:1.6.1'
44 | implementation 'com.google.android.material:material:1.9.0'
45 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
46 | testImplementation 'junit:junit:4.13.2'
47 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
49 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/demo/maple/fitsystembardemo/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package demo.maple.fitsystembardemo
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("demo.maple.fitsystembardemo", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
15 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/java/demo/maple/fitsystembardemo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package demo.maple.fitsystembardemo
2 |
3 | import android.graphics.Color
4 | import android.os.Build
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import androidx.core.view.WindowCompat
8 | import androidx.core.view.WindowInsetsCompat
9 | import androidx.core.view.WindowInsetsControllerCompat
10 | import androidx.core.view.isVisible
11 | import androidx.databinding.DataBindingUtil
12 | import com.xiaobin.fitsystembar.FitSystemBarHelper
13 | import com.xiaobin.fitsystembar.FitSystemBarHelper.Companion.fitSystemBarHelper
14 | import com.xiaobin.fitsystembar.setSystemBarTheme
15 | import com.xiaobin.fitsystembar.showNavigationBar
16 | import com.xiaobin.fitsystembar.showStatusBar
17 | import demo.maple.fitsystembardemo.databinding.ActivityMainBinding
18 |
19 | /**
20 | * AndroidManifest.xml中给activity配置了:
21 | * android:windowSoftInputMode="adjustResize"
22 | */
23 | class MainActivity : AppCompatActivity() {
24 |
25 | private val binding by lazy {
26 | DataBindingUtil.setContentView(this, R.layout.activity_main)
27 | }
28 |
29 | override fun onCreate(savedInstanceState: Bundle?) {
30 | super.onCreate(savedInstanceState)
31 | // 任意View实现FitSystemBar效果
32 | // 中间内容区域上下两边不需要填充,因为上面有标题栏,下面有输入框栏
33 | FitSystemBarHelper.attachView(
34 | binding.content,
35 | start = true,
36 | top = false,
37 | end = true,
38 | bottom = false
39 | )
40 | //第二种写法, 这种写法支持动态改变,但改变后的生效时间由系统分发WindowInsets的时机决定
41 | //如果需要立即生效,请在改变值后调用 fitSystemHelperForContent.requestApplyInsets()
42 | FitSystemBarHelper.attachView(binding.content) { orientation ->
43 | when (orientation) {
44 | FitSystemBarHelper.Orientation.Start -> true
45 | FitSystemBarHelper.Orientation.Top -> false
46 | FitSystemBarHelper.Orientation.End -> true
47 | FitSystemBarHelper.Orientation.Bottom -> false
48 | }
49 | }
50 | binding.llButtons2.isVisible = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
51 | binding.btnSystemBarShow.setOnClickListener {
52 | showSystemBar(true)
53 | }
54 | binding.btnSystemBarHide.setOnClickListener {
55 | showSystemBar(false)
56 | }
57 | binding.btnStatusBarShow.setOnClickListener {
58 | window.showStatusBar(true)
59 | }
60 | binding.btnStatusBarHide.setOnClickListener {
61 | window.showStatusBar(false)
62 | }
63 | binding.btnNaviBarShow.setOnClickListener {
64 | window.showNavigationBar(true)
65 | }
66 | binding.btnNaviBarHide.setOnClickListener {
67 | window.showNavigationBar(false)
68 | }
69 | binding.btnSystemCutOut.setOnClickListener {
70 | cutOutSet(true)
71 | }
72 | binding.btnSystemNoCutOut.setOnClickListener {
73 | cutOutSet(false)
74 | }
75 | binding.btnSmoothPadding.setOnClickListener {
76 | smoothPadding(true)
77 | }
78 | binding.btnNoSmoothPadding.setOnClickListener {
79 | smoothPadding(false)
80 | }
81 | }
82 |
83 | private fun smoothPadding(enable: Boolean) {
84 | binding.titleBar.smoothPadding(enable)
85 | binding.navigationBar.smoothPadding(enable)
86 | binding.content.fitSystemBarHelper()?.smoothPadding = enable
87 | }
88 |
89 | private fun cutOutSet(enable: Boolean) {
90 | binding.titleBar.safeCutOutPadding(enable)
91 | binding.navigationBar.safeCutOutPadding(enable)
92 | binding.content.fitSystemBarHelper()?.safeCutOutPadding = enable
93 | }
94 |
95 | private fun showSystemBar(show: Boolean) {
96 | val insetsController = WindowCompat.getInsetsController(window, window.decorView)
97 | insetsController.systemBarsBehavior =
98 | WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
99 | if (show) {
100 | insetsController.show(WindowInsetsCompat.Type.systemBars())
101 | } else {
102 | insetsController.hide(WindowInsetsCompat.Type.systemBars())
103 | }
104 | }
105 |
106 | override fun onResume() {
107 | super.onResume()
108 | window.setSystemBarTheme(false, false)
109 | //沉浸式状态栏
110 | WindowCompat.setDecorFitsSystemWindows(window, false)
111 | val insetsController = WindowCompat.getInsetsController(window, window.decorView)
112 | insetsController.isAppearanceLightStatusBars = false
113 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
114 | //状态栏设置透明
115 | window.statusBarColor = Color.TRANSPARENT
116 | }
117 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
118 | window.isNavigationBarContrastEnforced = false
119 | }
120 | insetsController.isAppearanceLightNavigationBars = false
121 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
122 | window.navigationBarColor = Color.TRANSPARENT
123 | } else {
124 | window.navigationBarColor = Color.BLACK
125 | }
126 | }
127 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
14 |
15 |
21 |
22 |
28 |
29 |
35 |
36 |
44 |
45 |
46 |
47 |
53 |
54 |
60 |
61 |
67 |
68 |
78 |
79 |
87 |
88 |
94 |
95 |
96 |
97 |
108 |
109 |
117 |
118 |
124 |
125 |
126 |
127 |
138 |
139 |
140 |
150 |
151 |
156 |
157 |
162 |
163 |
164 |
165 |
175 |
176 |
181 |
182 |
187 |
188 |
189 |
190 |
201 |
202 |
207 |
208 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
226 |
227 |
240 |
241 |
242 |
243 |
244 |
245 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values-night-v27/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values-v27/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FF000000
4 | #FFFFFFFF
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FitSystemBarDemo
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/app/src/test/java/demo/maple/fitsystembardemo/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package demo.maple.fitsystembardemo
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | plugins {
3 | id 'com.android.application' version '8.1.1' apply false
4 | id 'com.android.library' version '8.1.1' apply false
5 | id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
6 | }
--------------------------------------------------------------------------------
/fitSystemBar/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/fitSystemBar/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | id 'kotlin-kapt'
5 | }
6 |
7 | android {
8 | namespace 'com.xiaobin.fitsystembar'
9 | compileSdk 33
10 |
11 | defaultConfig {
12 | minSdk 21
13 | targetSdk 33
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | consumerProguardFiles "consumer-rules.pro"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_17
27 | targetCompatibility JavaVersion.VERSION_17
28 | }
29 | kotlinOptions {
30 | jvmTarget = '17'
31 | }
32 | buildFeatures {
33 | dataBinding = true
34 | }
35 | }
36 |
37 | dependencies {
38 | compileOnly 'androidx.core:core-ktx:1.12.0'
39 | compileOnly 'androidx.appcompat:appcompat:1.6.1'
40 | // compileOnly 'com.google.android.material:material:1.9.0'
41 | compileOnly 'androidx.constraintlayout:constraintlayout:2.1.4'
42 | }
--------------------------------------------------------------------------------
/fitSystemBar/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/fitSystemBar/consumer-rules.pro
--------------------------------------------------------------------------------
/fitSystemBar/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/fitSystemBar/src/androidTest/java/com/xiaobin/fitsystembar/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.xiaobin.fitsystembar.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/FitSystemBarHelper.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar
2 |
3 | import android.os.Build
4 | import android.util.Log
5 | import android.view.View
6 | import android.view.View.OnAttachStateChangeListener
7 | import androidx.core.view.ViewCompat
8 | import androidx.core.view.WindowInsetsAnimationCompat
9 | import androidx.core.view.WindowInsetsCompat
10 | import androidx.databinding.BindingAdapter
11 | import kotlin.math.max
12 |
13 | /**
14 | * 处理系统栏的遮挡问题
15 | * 原理是通过监听insets属性的变化得知系统栏的可见的高度
16 | * 同时这里也处理了挖孔屏的问题,但只是简单处理
17 | * 如果屏幕包含挖孔,且safeCutOutPadding没有设为false,则有挖孔的那条边,无论系统栏是否可见,
18 | * 都会被附加一个挖孔高度的padding
19 | * 当系统栏可见并且有挖孔时,padding取最大的
20 | *
21 | * 注意: 绑定的view在xml中请勿使用paddingLeft、paddingRight,请使用paddingStart、paddingEnd
22 | *
23 | * Tips: 系统栏包含 底部虚拟导航栏 和 顶部状态栏 以及 软键盘
24 | * 平滑填充指系统栏在显示和隐藏的动画执行过程中,实时填充
25 | * @author matcha_xiaobin
26 | */
27 | class FitSystemBarHelper(
28 | private val view: View,
29 | private val TAG: String = "FitSystemBarHelper",
30 | private val callBack: (orientation: Orientation) -> Boolean
31 | ) {
32 |
33 | internal lateinit var initialPadding: RelativePadding
34 | private set
35 |
36 | //当前是否正在平滑填充中
37 | var inSmoothingPadding = false
38 | private set
39 |
40 | //是否需要处理挖孔屏,刘海屏
41 | var safeCutOutPadding: Boolean = true
42 | set(value) {
43 | field = value
44 | if (ViewCompat.isAttachedToWindow(view)) {
45 | ViewCompat.requestApplyInsets(view)
46 | }
47 | }
48 |
49 | //是否需要平滑填充,仅在android R及以上生效,默认开启
50 | var smoothPadding: Boolean = true
51 |
52 | //是否需要打印日志
53 | var debug = true
54 |
55 | enum class Orientation {
56 | Start, Top, End, Bottom
57 | }
58 |
59 | companion object {
60 |
61 | /**
62 | * 获取这个view已绑定的FitSystemBarHelper对象
63 | */
64 | fun View.fitSystemBarHelper(): FitSystemBarHelper? {
65 | val helper = this.getTag(R.id.fit_system_helper)
66 | if (helper is FitSystemBarHelper) return helper
67 | return null
68 | }
69 |
70 | /**
71 | * 绑定到View
72 | * @param view 指定View
73 | * @param callBack 用于判断是否需要某条边的padding,会非常频繁的调用,所以不要在里面做需要耗时的操作,以免卡UI线程
74 | */
75 | fun attachView(view: View, callBack: (orientation: Orientation) -> Boolean) =
76 | FitSystemBarHelper(view, callBack = callBack)
77 |
78 | /**
79 | * 绑定到View
80 | * @param view 指定View
81 | * @param start 是否需要paddingStart
82 | * @param top 是否需要paddingTop
83 | * @param end 是否需要paddingEnd
84 | * @param bottom 是否需要paddingBottom
85 | */
86 | fun attachView(
87 | view: View,
88 | start: Boolean = false,
89 | top: Boolean = false,
90 | end: Boolean = false,
91 | bottom: Boolean = false
92 | ) = attachView(view) {
93 | when (it) {
94 | Orientation.Start -> start
95 | Orientation.Top -> top
96 | Orientation.End -> end
97 | Orientation.Bottom -> bottom
98 | }
99 | }
100 |
101 | /**
102 | * 绑定到View
103 | * @param start 是否需要paddingStart
104 | * @param top 是否需要paddingTop
105 | * @param end 是否需要paddingEnd
106 | * @param bottom 是否需要paddingBottom
107 | */
108 | @BindingAdapter(
109 | value = ["fitStart", "fitTop", "fitEnd", "fitBottom"],
110 | requireAll = false
111 | )
112 | fun View.fitSystemBar(
113 | start: Boolean = false,
114 | top: Boolean = false,
115 | end: Boolean = false,
116 | bottom: Boolean = false
117 | ) {
118 | attachView(this) {
119 | when (it) {
120 | Orientation.Start -> start
121 | Orientation.Top -> top
122 | Orientation.End -> end
123 | Orientation.Bottom -> bottom
124 | }
125 | }
126 | }
127 |
128 | }
129 |
130 | private var attachStateChangeListener: OnAttachStateChangeListener =
131 | object : OnAttachStateChangeListener {
132 | override fun onViewAttachedToWindow(v: View) {
133 | v.removeOnAttachStateChangeListener(this)
134 | ViewCompat.requestApplyInsets(v)
135 | }
136 |
137 | override fun onViewDetachedFromWindow(v: View) {}
138 | }
139 |
140 | init {
141 | attach()
142 | }
143 |
144 | fun requestApplyInsets() {
145 | if (ViewCompat.isAttachedToWindow(view)) {
146 | ViewCompat.requestApplyInsets(view)
147 | }
148 | }
149 |
150 | /**
151 | * 添加绑定
152 | */
153 | fun attach() {
154 | //一个view只让绑定一个helper,如果多个绑定同一个view,则取消之前的
155 | val helper = view.getTag(R.id.fit_system_helper)
156 | if (helper == this) return
157 | initialPadding = if (helper is FitSystemBarHelper) {
158 | helper.destory()
159 | //获取原始padding的快照
160 | RelativePadding(helper.initialPadding)
161 | } else {
162 | //创建原始padding的快照
163 | RelativePadding(
164 | ViewCompat.getPaddingStart(view),
165 | view.paddingTop,
166 | ViewCompat.getPaddingEnd(view),
167 | view.paddingBottom
168 | )
169 | }
170 | view.setTag(R.id.fit_system_helper, this@FitSystemBarHelper)
171 | view.fitsSystemWindows = false
172 | //不带平滑变化的
173 | ViewCompat.setOnApplyWindowInsetsListener(view) { view: View, insets: WindowInsetsCompat ->
174 | //当正在平滑填充时,不做处理,否则你会看到这个view在反复弹跳
175 | if (inSmoothingPadding) return@setOnApplyWindowInsetsListener insets
176 | //即使开启了平滑填充,这个也必须存在,否则,一些情况下会导致没有填充而被系统栏遮挡
177 | formatInsets(insets, RelativePadding(initialPadding))
178 | insets
179 | }
180 | //虽然ViewCompat中帮我们实现了低于 R 的版本也能走...输入法动画,但会导致其它的一些问题
181 | //因为低于R的设备上,WindowInsets中的系统栏insets获取到的isVisible将会始终为true
182 | //导致获取的高度在 隐藏了系统栏 的情况下,依然等于 可见状态下 的系统栏高度
183 | //然后再旋转屏幕之后,并不能正确的将设置了底部虚拟导航键的高度的padding值没有被清掉
184 | //所以在低于api 30的设备上干脆禁用好了
185 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
186 | //带平滑变化的
187 | ViewCompat.setWindowInsetsAnimationCallback(
188 | view,
189 | object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
190 |
191 | override fun onProgress(
192 | insets: WindowInsetsCompat,
193 | runningAnimations: List
194 | ): WindowInsetsCompat {
195 | if (smoothPadding) {
196 | formatInsets(insets, RelativePadding(initialPadding))
197 | }
198 | return insets
199 | }
200 |
201 | override fun onEnd(animation: WindowInsetsAnimationCompat) {
202 | inSmoothingPadding = false
203 | super.onEnd(animation)
204 | }
205 |
206 | override fun onPrepare(animation: WindowInsetsAnimationCompat) {
207 | inSmoothingPadding = smoothPadding
208 | super.onPrepare(animation)
209 | }
210 | })
211 | }
212 | if (ViewCompat.isAttachedToWindow(view)) {
213 | ViewCompat.requestApplyInsets(view)
214 | } else {
215 | view.addOnAttachStateChangeListener(attachStateChangeListener)
216 | }
217 | }
218 |
219 | /**
220 | * 针对不同版本处理Insets
221 | */
222 | private fun formatInsets(
223 | insetsCompat: WindowInsetsCompat,
224 | initialPadding: RelativePadding
225 | ) {
226 | var cutoutPaddingLeft = 0
227 | var cutoutPaddingTop = 0
228 | var cutoutPaddingRight = 0
229 | var cutoutPaddingBottom = 0
230 |
231 | var systemWindowInsetLeft = 0
232 | var systemWindowInsetTop = 0
233 | var systemWindowInsetRight = 0
234 | var systemWindowInsetBottom = 0
235 |
236 | if (safeCutOutPadding) {
237 | insetsCompat.displayCutout?.let {
238 | cutoutPaddingTop = it.safeInsetTop
239 | cutoutPaddingLeft = it.safeInsetLeft
240 | cutoutPaddingRight = it.safeInsetRight
241 | cutoutPaddingBottom = it.safeInsetBottom
242 | }
243 | }
244 | val systemBars = insetsCompat.getInsets(
245 | WindowInsetsCompat.Type.ime()
246 | or WindowInsetsCompat.Type.systemBars()
247 | )
248 | systemWindowInsetLeft = systemBars.left
249 | systemWindowInsetRight = systemBars.right
250 |
251 | //对api低于30的设备,做额外判断,api 30+的不需要这个
252 | val suv = view.rootView.windowSystemUiVisibility
253 | val statusBar = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
254 | suv and View.SYSTEM_UI_FLAG_FULLSCREEN == 0
255 | } else true
256 | val naviBar = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
257 | suv and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION == 0
258 | } else true
259 | if (
260 | naviBar &&
261 | (insetsCompat.isVisible(WindowInsetsCompat.Type.ime())
262 | || insetsCompat.isVisible(WindowInsetsCompat.Type.navigationBars()))
263 | ) {
264 | systemWindowInsetBottom = systemBars.bottom
265 | }
266 | if (
267 | statusBar &&
268 | insetsCompat.isVisible(WindowInsetsCompat.Type.statusBars())
269 | ) {
270 | systemWindowInsetTop = systemBars.top
271 | }
272 |
273 | if (callBack.invoke(Orientation.Top)) {
274 | initialPadding.top += max(systemWindowInsetTop, cutoutPaddingTop)
275 | }
276 | if (callBack.invoke(Orientation.Bottom)) {
277 | initialPadding.bottom += max(systemWindowInsetBottom, cutoutPaddingBottom)
278 | }
279 |
280 | val isRtl =
281 | ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL
282 | if (callBack.invoke(Orientation.Start)) {
283 | initialPadding.start += if (isRtl) {
284 | max(systemWindowInsetRight, cutoutPaddingRight)
285 | } else {
286 | max(systemWindowInsetLeft, cutoutPaddingLeft)
287 | }
288 | }
289 | if (callBack.invoke(Orientation.End)) {
290 | initialPadding.end += if (isRtl) {
291 | max(systemWindowInsetLeft, cutoutPaddingLeft)
292 | } else {
293 | max(systemWindowInsetRight, cutoutPaddingRight)
294 | }
295 | }
296 | initialPadding.applyToView(view)
297 | log(
298 | "处理Insets后的padding: " +
299 | "Start: " + initialPadding.start + ", " +
300 | "Top: " + initialPadding.top + ", " +
301 | "End: " + initialPadding.end + ", " +
302 | "Bottom: " + initialPadding.bottom + "\n" +
303 | "Insets的padding: " +
304 | "Left: " + systemWindowInsetLeft + ", " +
305 | "Top: " + systemWindowInsetTop + ", " +
306 | "Right: " + systemWindowInsetRight + ", " +
307 | "Bottom: " + systemWindowInsetBottom
308 | )
309 | }
310 |
311 | private fun log(content: Any) {
312 | if (debug) {
313 | Log.d(TAG, "log: $content")
314 | }
315 | }
316 |
317 | fun destory() {
318 | //取消所有监听
319 | view.removeOnAttachStateChangeListener(attachStateChangeListener)
320 | ViewCompat.setOnApplyWindowInsetsListener(view, null)
321 | ViewCompat.setWindowInsetsAnimationCallback(view, null)
322 | //恢复原始padding
323 | initialPadding.applyToView(view)
324 | //移除tag
325 | view.setTag(R.id.fit_system_helper, null)
326 | }
327 |
328 | internal class RelativePadding {
329 | var start: Int
330 | var top: Int
331 | var end: Int
332 | var bottom: Int
333 |
334 | constructor(start: Int, top: Int, end: Int, bottom: Int) {
335 | this.start = start
336 | this.top = top
337 | this.end = end
338 | this.bottom = bottom
339 | }
340 |
341 | constructor(other: RelativePadding) {
342 | start = other.start
343 | top = other.top
344 | end = other.end
345 | bottom = other.bottom
346 | }
347 |
348 | /**
349 | * Applies this relative padding to the view.
350 | */
351 | fun applyToView(view: View?) {
352 | ViewCompat.setPaddingRelative(view!!, start, top, end, bottom)
353 | }
354 | }
355 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/SystemBarExtensions.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar
2 |
3 | import android.graphics.Color
4 | import android.os.Build
5 | import android.view.View
6 | import android.view.Window
7 | import android.view.WindowManager
8 | import androidx.core.view.ViewCompat
9 | import androidx.core.view.WindowCompat
10 | import androidx.core.view.WindowInsetsCompat
11 | import androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
12 |
13 | /**
14 | * 系统UI沉浸式工具箱
15 | * 需要依赖Androidx:
16 | * androidx.core:core-ktx:1.5.0 或者更高
17 | *
18 | * @author xiaobin
19 | * @data 2022/6/30 00:00
20 | */
21 |
22 | /**
23 | * 附带一个工具
24 | * 允许或禁止 在当前app界面 截屏 和 录屏
25 | */
26 | fun Window.disabledScreenShot(disabled: Boolean = true) {
27 | if (disabled) {
28 | //设置禁止截屏、录屏标志
29 | addFlags(WindowManager.LayoutParams.FLAG_SECURE)
30 | } else {
31 | //清除禁止截屏、录屏标志
32 | clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
33 | }
34 | }
35 |
36 | /**
37 | * 显示或隐藏软键盘,但是必须界面上有可以输入的控件并且可以获取到焦点才会生效
38 | */
39 | fun Window.showIme(view: View = decorView, show: Boolean = true) {
40 | val insetsController = WindowCompat.getInsetsController(this, view)
41 | if (show) {
42 | insetsController.show(WindowInsetsCompat.Type.ime())
43 | } else {
44 | insetsController.hide(WindowInsetsCompat.Type.ime())
45 | }
46 | }
47 |
48 | /**
49 | * 显示或隐藏 captionBar
50 | */
51 | fun Window.showCaptionBar(view: View = decorView, show: Boolean = true) {
52 | val insetsController = WindowCompat.getInsetsController(this, view)
53 | if (show) {
54 | insetsController.show(WindowInsetsCompat.Type.captionBar())
55 | } else {
56 | insetsController.hide(WindowInsetsCompat.Type.captionBar())
57 | }
58 | }
59 |
60 | /**
61 | * 控制系统状态栏和导航栏显隐
62 | * @param behavior BEHAVIOR_SHOW_BARS_BY_TOUCH 任何操作都将会重新将隐藏了的系统栏显示出来
63 | * BEHAVIOR_SHOW_BARS_BY_SWIPE 边缘滑动向内滑动会重新将隐藏了的系统栏显示出来
64 | * BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 边缘滑动向内滑动会重新将隐藏了的系统栏显示出来,
65 | * 但过一会儿会重新自动隐藏
66 | */
67 | fun Window.showSystemBar(
68 | show: Boolean,
69 | behavior: Int = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
70 | ) {
71 | val insetsController = WindowCompat.getInsetsController(this, decorView)
72 | insetsController.systemBarsBehavior = behavior
73 | if (show) {
74 | insetsController.show(WindowInsetsCompat.Type.systemBars())
75 | } else {
76 | insetsController.hide(WindowInsetsCompat.Type.systemBars())
77 | }
78 | }
79 |
80 | /**
81 | * 控制状态栏显隐
82 | * @param behavior 参考 showSystemBar 的描述
83 | */
84 | fun Window.showStatusBar(
85 | show: Boolean,
86 | behavior: Int = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
87 | ) {
88 | val insetsController = WindowCompat.getInsetsController(this, decorView)
89 | insetsController.systemBarsBehavior = behavior
90 | if (show) {
91 | insetsController.show(WindowInsetsCompat.Type.statusBars())
92 | } else {
93 | insetsController.hide(WindowInsetsCompat.Type.statusBars())
94 | }
95 | }
96 |
97 | /**
98 | * 控制导航栏显隐
99 | * @param behavior 参考 showSystemBar 的描述
100 | */
101 | fun Window.showNavigationBar(
102 | show: Boolean,
103 | behavior: Int = BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
104 | ) {
105 | val insetsController = WindowCompat.getInsetsController(this, decorView)
106 | insetsController.systemBarsBehavior = behavior
107 | if (show) {
108 | insetsController.show(WindowInsetsCompat.Type.navigationBars())
109 | } else {
110 | insetsController.hide(WindowInsetsCompat.Type.navigationBars())
111 | }
112 | }
113 |
114 | /**
115 | * 状态栏以及虚拟导航栏图标主题
116 | * @param lightStatusBar 状态栏 true: 浅色背景 false: 深色背景
117 | * @param lightNavigationBar 导航栏 true: 浅色背景 false: 深色背景
118 | */
119 | fun Window.setSystemBarTheme(
120 | lightStatusBar: Boolean,
121 | lightNavigationBar: Boolean = lightStatusBar
122 | ) {
123 | //低于6.0的不做处理了
124 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
125 | //先开启全屏模式
126 | WindowCompat.setDecorFitsSystemWindows(this, false)
127 | //状态栏相关,仅android 6.0+生效
128 | setStatusBarTheme(lightStatusBar)
129 | //虚拟导航键相关,仅android 8.0+生效
130 | setNavigationBarTheme(lightNavigationBar)
131 | }
132 | }
133 |
134 | /**
135 | * 状态栏图标主题
136 | * 仅在Android6.0以上才官方支持状态栏深色浅色转换
137 | *
138 | * @param mask 是否增加遮罩 (正常情况下,状态栏图标是白色,遮罩将会使其变为黑色)
139 | */
140 | fun Window.setStatusBarTheme(mask: Boolean) {
141 | val insetsController = WindowCompat.getInsetsController(this, decorView)
142 | insetsController.isAppearanceLightStatusBars = mask
143 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
144 | //状态栏设置透明
145 | statusBarColor = Color.TRANSPARENT
146 | }
147 | }
148 |
149 | /**
150 | * 导航栏图标主题
151 | * 仅在Android8.0以上才官方支持导航栏深色浅色转换
152 | *
153 | * @param window 需要设置的窗口
154 | * @param mask 是否增加遮罩 (正常情况下,导航栏图标是白色,遮罩将会使其变为黑色,但国内UI似乎和谷歌原生处理方式不一致)
155 | */
156 | fun Window.setNavigationBarTheme(mask: Boolean) {
157 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
158 | isNavigationBarContrastEnforced = false
159 | }
160 | val insetsController = WindowCompat.getInsetsController(this, decorView)
161 | insetsController.isAppearanceLightNavigationBars = mask
162 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
163 | navigationBarColor = Color.TRANSPARENT
164 | } else {
165 | navigationBarColor = Color.BLACK
166 | }
167 | }
168 |
169 | /**
170 | * 获取状态栏高度,未测试
171 | */
172 | fun Window.getStatusBarHeight(): Int {
173 | if (ViewCompat.getFitsSystemWindows(decorView)) {
174 | return decorView.paddingTop
175 | }
176 | return ViewCompat.getRootWindowInsets(decorView)
177 | ?.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.statusBars())
178 | ?.top
179 | ?: -1
180 | }
181 |
182 | /**
183 | * 判断颜色是否为浅色
184 | *
185 | * @param color
186 | * @return
187 | */
188 | fun Int.isLightColor(): Boolean {
189 | val darkness =
190 | 1 - (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255
191 | return darkness < 0.5
192 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/base/FitSystemBarConstraintLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.base
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import androidx.constraintlayout.widget.ConstraintLayout
6 | import androidx.core.view.ViewCompat
7 | import com.xiaobin.fitsystembar.FitSystemBarHelper
8 | import com.xiaobin.fitsystembar.R
9 | import com.xiaobin.fitsystembar.listener.OnLayoutRectChangeListener
10 |
11 | /**
12 | * 可自由控制 系统 padding属性的约束布局
13 | */
14 | open class FitSystemBarConstraintLayout @JvmOverloads constructor(
15 | context: Context,
16 | val attrs: AttributeSet? = null,
17 | defStyleAttr: Int = 0
18 | ) : ConstraintLayout(context, attrs, defStyleAttr) {
19 |
20 | protected val TAG by lazy {
21 | this.javaClass.simpleName
22 | }
23 |
24 | protected var paddingTopSystemWindowInsets: Boolean? = null
25 | protected var paddingBottomSystemWindowInsets: Boolean? = null
26 | protected var paddingStartSystemWindowInsets: Boolean? = null
27 | protected var paddingEndSystemWindowInsets: Boolean? = null
28 |
29 | private val helper by lazy {
30 | FitSystemBarHelper(this, TAG) {
31 | when (it) {
32 | FitSystemBarHelper.Orientation.Start -> shouldApplyWindowInsetPadding(
33 | paddingStartSystemWindowInsets
34 | )
35 |
36 | FitSystemBarHelper.Orientation.Top -> shouldApplyWindowInsetPadding(
37 | paddingTopSystemWindowInsets
38 | )
39 |
40 | FitSystemBarHelper.Orientation.End -> shouldApplyWindowInsetPadding(
41 | paddingEndSystemWindowInsets
42 | )
43 |
44 | FitSystemBarHelper.Orientation.Bottom -> shouldApplyWindowInsetPadding(
45 | paddingBottomSystemWindowInsets
46 | )
47 | }
48 | }
49 | }
50 |
51 | fun safeCutOutPadding(enable: Boolean) {
52 | helper.safeCutOutPadding = enable
53 | }
54 |
55 | fun smoothPadding(enable: Boolean) {
56 | helper.smoothPadding = enable
57 | }
58 |
59 | fun enableDebugLogger(enable: Boolean) {
60 | helper.debug = enable
61 | }
62 |
63 | fun updateSystemPaddingInsets(start: Boolean, top: Boolean, end: Boolean, bottom: Boolean) {
64 | paddingTopSystemWindowInsets = top
65 | paddingBottomSystemWindowInsets = bottom
66 | paddingStartSystemWindowInsets = start
67 | paddingEndSystemWindowInsets = end
68 | ViewCompat.requestApplyInsets(this)
69 | }
70 |
71 | init {
72 | attrs?.let {
73 | val a =
74 | getContext().obtainStyledAttributes(attrs, R.styleable.FitSystemBarConstraintLayout)
75 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemTop)) {
76 | paddingTopSystemWindowInsets =
77 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemTop, true)
78 | }
79 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemBottom)) {
80 | paddingBottomSystemWindowInsets =
81 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemBottom, true)
82 | }
83 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemStart)) {
84 | paddingStartSystemWindowInsets =
85 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemStart, true)
86 | }
87 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemEnd)) {
88 | paddingEndSystemWindowInsets =
89 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemEnd, true)
90 | }
91 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_safeCutOutPadding)) {
92 | helper.safeCutOutPadding =
93 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_safeCutOutPadding, true)
94 | }
95 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_debug)) {
96 | helper.debug =
97 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_debug, false)
98 | }
99 | a.recycle()
100 | }
101 | helper.attach()
102 | }
103 |
104 | private var onLayoutRectChangeListener = mutableListOf()
105 |
106 | fun addOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
107 | if (!onLayoutRectChangeListener.contains(listener)) {
108 | onLayoutRectChangeListener.add(listener)
109 | }
110 | }
111 |
112 | fun removeOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
113 | if (onLayoutRectChangeListener.contains(listener)) {
114 | onLayoutRectChangeListener.remove(listener)
115 | }
116 | }
117 |
118 | /**
119 | * Whether the top or bottom of this view should be padded in to avoid the system window insets.
120 | *
121 | * If the `paddingInsetFlag` is set, that value will take precedent. Otherwise,
122 | * fitsSystemWindow will be used.
123 | */
124 | private fun shouldApplyWindowInsetPadding(paddingInsetFlag: Boolean?): Boolean {
125 | return paddingInsetFlag ?: true
126 | }
127 |
128 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
129 | super.onLayout(changed, left, top, right, bottom)
130 | onLayoutRectChangeListener.forEach {
131 | it.onLayoutChange(changed, left, top, right, bottom)
132 | }
133 | }
134 |
135 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/base/FitSystemBarFrameLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.base
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.widget.FrameLayout
6 | import androidx.core.view.ViewCompat
7 | import com.xiaobin.fitsystembar.R
8 | import com.xiaobin.fitsystembar.FitSystemBarHelper
9 | import com.xiaobin.fitsystembar.listener.OnLayoutRectChangeListener
10 |
11 | /**
12 | * 设置paddingTop为顶部系统栏的高度,其它方向的padding不变
13 | */
14 | open class FitSystemBarFrameLayout @JvmOverloads constructor(
15 | context: Context,
16 | val attrs: AttributeSet? = null,
17 | defStyleAttr: Int = 0
18 | ) : FrameLayout(context, attrs, defStyleAttr) {
19 |
20 | protected val TAG by lazy {
21 | this.javaClass.simpleName
22 | }
23 |
24 | protected var paddingTopSystemWindowInsets: Boolean? = null
25 | protected var paddingBottomSystemWindowInsets: Boolean? = null
26 | protected var paddingStartSystemWindowInsets: Boolean? = null
27 | protected var paddingEndSystemWindowInsets: Boolean? = null
28 |
29 | private val helper by lazy {
30 | FitSystemBarHelper(this, TAG) {
31 | when (it) {
32 | FitSystemBarHelper.Orientation.Start -> shouldApplyWindowInsetPadding(
33 | paddingStartSystemWindowInsets
34 | )
35 |
36 | FitSystemBarHelper.Orientation.Top -> shouldApplyWindowInsetPadding(
37 | paddingTopSystemWindowInsets
38 | )
39 |
40 | FitSystemBarHelper.Orientation.End -> shouldApplyWindowInsetPadding(
41 | paddingEndSystemWindowInsets
42 | )
43 |
44 | FitSystemBarHelper.Orientation.Bottom -> shouldApplyWindowInsetPadding(
45 | paddingBottomSystemWindowInsets
46 | )
47 | }
48 | }
49 | }
50 |
51 | fun safeCutOutPadding(enable: Boolean) {
52 | helper.safeCutOutPadding = enable
53 | }
54 |
55 | fun smoothPadding(enable: Boolean) {
56 | helper.smoothPadding = enable
57 | }
58 |
59 | fun enableDebugLogger(enable: Boolean) {
60 | helper.debug = enable
61 | }
62 |
63 | fun updateSystemPaddingInsets(start: Boolean, top: Boolean, end: Boolean, bottom: Boolean) {
64 | paddingTopSystemWindowInsets = top
65 | paddingBottomSystemWindowInsets = bottom
66 | paddingStartSystemWindowInsets = start
67 | paddingEndSystemWindowInsets = end
68 | ViewCompat.requestApplyInsets(this)
69 | }
70 |
71 | init {
72 | attrs?.let {
73 | val a = getContext().obtainStyledAttributes(attrs, R.styleable.FitSystemBarFrameLayout)
74 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemTop)) {
75 | paddingTopSystemWindowInsets =
76 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemTop, true)
77 | }
78 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemBottom)) {
79 | paddingBottomSystemWindowInsets =
80 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemBottom, true)
81 | }
82 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemStart)) {
83 | paddingStartSystemWindowInsets =
84 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemStart, true)
85 | }
86 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_fitSystemEnd)) {
87 | paddingEndSystemWindowInsets =
88 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_fitSystemEnd, true)
89 | }
90 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_safeCutOutPadding)) {
91 | helper.safeCutOutPadding =
92 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_safeCutOutPadding, true)
93 | }
94 | if (a.hasValue(R.styleable.FitSystemBarFrameLayout_fs_debug)) {
95 | helper.debug =
96 | a.getBoolean(R.styleable.FitSystemBarFrameLayout_fs_debug, false)
97 | }
98 | a.recycle()
99 | }
100 | helper.attach()
101 | }
102 |
103 | private var onLayoutRectChangeListener = mutableListOf()
104 |
105 | fun addOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
106 | if (!onLayoutRectChangeListener.contains(listener)) {
107 | onLayoutRectChangeListener.add(listener)
108 | }
109 | }
110 |
111 | fun removeOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
112 | if (onLayoutRectChangeListener.contains(listener)) {
113 | onLayoutRectChangeListener.remove(listener)
114 | }
115 | }
116 |
117 | /**
118 | * Whether the top or bottom of this view should be padded in to avoid the system window insets.
119 | *
120 | * If the `paddingInsetFlag` is set, that value will take precedent. Otherwise,
121 | * fitsSystemWindow will be used.
122 | */
123 | private fun shouldApplyWindowInsetPadding(paddingInsetFlag: Boolean?): Boolean {
124 | return paddingInsetFlag ?: true
125 | }
126 |
127 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
128 | super.onLayout(changed, left, top, right, bottom)
129 | onLayoutRectChangeListener.forEach {
130 | it.onLayoutChange(changed, left, top, right, bottom)
131 | }
132 | }
133 |
134 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/base/FitSystemBarLinearLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.base
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.widget.LinearLayout
6 | import androidx.core.view.ViewCompat
7 | import com.xiaobin.fitsystembar.R
8 | import com.xiaobin.fitsystembar.FitSystemBarHelper
9 | import com.xiaobin.fitsystembar.listener.OnLayoutRectChangeListener
10 |
11 | /**
12 | * 设置paddingTop为顶部系统栏的高度,其它方向的padding不变
13 | */
14 | open class FitSystemBarLinearLayout @JvmOverloads constructor(
15 | context: Context,
16 | val attrs: AttributeSet? = null,
17 | defStyleAttr: Int = 0
18 | ) : LinearLayout(context, attrs, defStyleAttr) {
19 |
20 | protected val TAG by lazy {
21 | this.javaClass.simpleName
22 | }
23 |
24 | protected var paddingTopSystemWindowInsets: Boolean? = null
25 | protected var paddingBottomSystemWindowInsets: Boolean? = null
26 | protected var paddingStartSystemWindowInsets: Boolean? = null
27 | protected var paddingEndSystemWindowInsets: Boolean? = null
28 |
29 | private var onLayoutRectChangeListener = mutableListOf()
30 |
31 | private val helper by lazy {
32 | FitSystemBarHelper(this, TAG) {
33 | when (it) {
34 | FitSystemBarHelper.Orientation.Start -> shouldApplyWindowInsetPadding(
35 | paddingStartSystemWindowInsets
36 | )
37 |
38 | FitSystemBarHelper.Orientation.Top -> shouldApplyWindowInsetPadding(
39 | paddingTopSystemWindowInsets
40 | )
41 |
42 | FitSystemBarHelper.Orientation.End -> shouldApplyWindowInsetPadding(
43 | paddingEndSystemWindowInsets
44 | )
45 |
46 | FitSystemBarHelper.Orientation.Bottom -> shouldApplyWindowInsetPadding(
47 | paddingBottomSystemWindowInsets
48 | )
49 | }
50 | }
51 | }
52 |
53 | fun safeCutOutPadding(enable: Boolean) {
54 | helper.safeCutOutPadding = enable
55 | }
56 |
57 | fun smoothPadding(enable: Boolean) {
58 | helper.smoothPadding = enable
59 | }
60 |
61 | fun enableDebugLogger(enable: Boolean) {
62 | helper.debug = enable
63 | }
64 |
65 | fun updateSystemPaddingInsets(start: Boolean, top: Boolean, end: Boolean, bottom: Boolean) {
66 | paddingTopSystemWindowInsets = top
67 | paddingBottomSystemWindowInsets = bottom
68 | paddingStartSystemWindowInsets = start
69 | paddingEndSystemWindowInsets = end
70 | ViewCompat.requestApplyInsets(this)
71 | }
72 |
73 | init {
74 | attrs?.let {
75 | val a = getContext().obtainStyledAttributes(attrs, R.styleable.FitSystemBarLinearLayout)
76 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_fitSystemTop)) {
77 | paddingTopSystemWindowInsets =
78 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_fitSystemTop, true)
79 | }
80 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_fitSystemBottom)) {
81 | paddingBottomSystemWindowInsets =
82 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_fitSystemBottom, true)
83 | }
84 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_fitSystemStart)) {
85 | paddingStartSystemWindowInsets =
86 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_fitSystemStart, true)
87 | }
88 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_fitSystemEnd)) {
89 | paddingEndSystemWindowInsets =
90 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_fitSystemEnd, true)
91 | }
92 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_safeCutOutPadding)) {
93 | helper.safeCutOutPadding =
94 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_safeCutOutPadding, true)
95 | }
96 | if (a.hasValue(R.styleable.FitSystemBarLinearLayout_fs_debug)) {
97 | helper.debug =
98 | a.getBoolean(R.styleable.FitSystemBarLinearLayout_fs_debug, false)
99 | }
100 | a.recycle()
101 | }
102 | helper.attach()
103 | }
104 |
105 | fun addOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
106 | if (!onLayoutRectChangeListener.contains(listener)) {
107 | onLayoutRectChangeListener.add(listener)
108 | }
109 | }
110 |
111 | fun removeOnLayoutRectChangeListener(listener: OnLayoutRectChangeListener) {
112 | if (onLayoutRectChangeListener.contains(listener)) {
113 | onLayoutRectChangeListener.remove(listener)
114 | }
115 | }
116 |
117 | /**
118 | * Whether the top or bottom of this view should be padded in to avoid the system window insets.
119 | *
120 | * If the `paddingInsetFlag` is set, that value will take precedent. Otherwise,
121 | * fitsSystemWindow will be used.
122 | */
123 | private fun shouldApplyWindowInsetPadding(paddingInsetFlag: Boolean?): Boolean {
124 | return paddingInsetFlag ?: true
125 | }
126 |
127 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
128 | super.onLayout(changed, left, top, right, bottom)
129 | onLayoutRectChangeListener.forEach {
130 | it.onLayoutChange(changed, left, top, right, bottom)
131 | }
132 | }
133 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/custom/FitNavigationBarFrameLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.custom
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import com.xiaobin.fitsystembar.base.FitSystemBarFrameLayout
6 |
7 | /**
8 | * 设置padding为系统栏的高度,但paddingTop方向的padding不变
9 | */
10 | class FitNavigationBarFrameLayout @JvmOverloads constructor(
11 | context: Context,
12 | attrs: AttributeSet? = null,
13 | defStyleAttr: Int = 0
14 | ) : FitSystemBarFrameLayout(context, attrs, defStyleAttr) {
15 |
16 | init {
17 | paddingTopSystemWindowInsets = false
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/custom/FitNavigationBarLinearLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.custom
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import com.xiaobin.fitsystembar.base.FitSystemBarLinearLayout
6 |
7 | /**
8 | * 设置padding为系统栏的高度,但paddingTop方向的padding不变
9 | */
10 | class FitNavigationBarLinearLayout @JvmOverloads constructor(
11 | context: Context,
12 | attrs: AttributeSet? = null,
13 | defStyleAttr: Int = 0
14 | ) : FitSystemBarLinearLayout(context, attrs, defStyleAttr) {
15 |
16 | init {
17 | paddingTopSystemWindowInsets = false
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/custom/FitStatusBarFrameLayout.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.custom
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import com.xiaobin.fitsystembar.base.FitSystemBarFrameLayout
6 |
7 | /**
8 | * 设置paddingTop为顶部系统栏的高度,其它方向的padding不变
9 | */
10 | class FitStatusBarFrameLayout @JvmOverloads constructor(
11 | context: Context,
12 | attrs: AttributeSet? = null,
13 | defStyleAttr: Int = 0
14 | ) : FitSystemBarFrameLayout(context, attrs, defStyleAttr) {
15 |
16 | init {
17 | paddingBottomSystemWindowInsets = false
18 | }
19 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/java/com/xiaobin/fitsystembar/listener/OnLayoutRectChangeListener.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar.listener
2 |
3 | interface OnLayoutRectChangeListener {
4 |
5 | fun onLayoutChange(isChange: Boolean, left: Int, top: Int, right: Int, bottom: Int)
6 | }
--------------------------------------------------------------------------------
/fitSystemBar/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/fitSystemBar/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/fitSystemBar/src/test/java/com/xiaobin/fitsystembar/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.xiaobin.fitsystembar
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/gif/screen.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/gif/screen.gif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Matcha-xiaobin/fitSystemBarUtils/05cc7e78c5bf80d208d9b86edbded01ebc9fa3bc/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jul 14 11:19:36 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google()
4 | mavenCentral()
5 | gradlePluginPortal()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "FitSystemBarDemo"
16 | include ':app'
17 | include ':fitSystemBar'
18 |
--------------------------------------------------------------------------------