11package com .hjq .demo .app ;
22
33import android .content .Intent ;
4- import android .graphics .Insets ;
54import android .view .View ;
6- import android .view .View .OnApplyWindowInsetsListener ;
7- import android .view .WindowInsets ;
85import androidx .annotation .NonNull ;
96import androidx .annotation .Nullable ;
107import androidx .annotation .StringRes ;
8+ import androidx .core .graphics .Insets ;
9+ import androidx .core .view .OnApplyWindowInsetsListener ;
10+ import androidx .core .view .ViewCompat ;
11+ import androidx .core .view .WindowInsetsCompat ;
12+ import androidx .lifecycle .LifecycleOwner ;
13+ import androidx .lifecycle .MutableLiveData ;
14+ import androidx .lifecycle .Observer ;
1115import com .gyf .immersionbar .ImmersionBar ;
1216import com .hjq .bar .TitleBar ;
1317import com .hjq .base .BaseActivity ;
14- import com .hjq .core .tools .AndroidVersion ;
1518import com .hjq .demo .R ;
1619import com .hjq .demo .action .ImmersionAction ;
1720import com .hjq .demo .action .TitleBarAction ;
@@ -35,6 +38,12 @@ public abstract class AppActivity extends BaseActivity
3538 private TitleBar mTitleBar ;
3639 /** 状态栏沉浸 */
3740 private ImmersionBar mImmersionBar ;
41+ /** 状态栏高度 LiveData */
42+ @ NonNull
43+ private final MutableLiveData <Integer > mStatusBarHeightLiveData = new MutableLiveData <>();
44+ /** 导航栏高度 LiveData */
45+ @ NonNull
46+ private final MutableLiveData <Integer > mNavigationBarHeightLiveData = new MutableLiveData <>();
3847
3948 /** 加载对话框 */
4049 private WaitDialog .Builder mDialog ;
@@ -110,38 +119,75 @@ protected void initLayout() {
110119 getStatusBarConfig ().init ();
111120 }
112121
113- // 适配 Android 15 EdgeToEdge 特性
114- if (AndroidVersion .isAndroid15 ()) {
115- getWindow ().getDecorView ().setOnApplyWindowInsetsListener (new OnApplyWindowInsetsListener () {
116-
117- @ NonNull
118- @ Override
119- public WindowInsets onApplyWindowInsets (@ NonNull View v , @ NonNull WindowInsets insets ) {
120- Insets systemBars = insets .getInsets (WindowInsets .Type .systemBars ());
121- View immersionTopView = getImmersionTopView ();
122- View immersionBottomView = getImmersionBottomView ();
123- if (immersionTopView != null && immersionTopView == immersionBottomView ) {
124- immersionTopView .setPadding (immersionTopView .getPaddingLeft (), systemBars .top ,
125- immersionTopView .getPaddingRight (), systemBars .bottom );
126- return insets ;
127- }
128- if (immersionTopView != null ) {
129- immersionTopView .setPadding (immersionTopView .getPaddingLeft (), systemBars .top ,
130- immersionTopView .getPaddingRight (), immersionTopView .getPaddingBottom ());
131- }
132- if (immersionBottomView != null ) {
133- immersionBottomView .setPadding (immersionBottomView .getPaddingLeft (), immersionBottomView .getPaddingTop (),
134- immersionBottomView .getPaddingRight (), systemBars .bottom );
135- }
136- return insets ;
137- }
138- });
139- } else {
122+ // 监听状态栏和导航栏高度变化
123+ mStatusBarHeightLiveData .observe (this , statusBarHeight -> {
124+ if (statusBarHeight == null ) {
125+ return ;
126+ }
140127 View immersionTopView = getImmersionTopView ();
141- if (immersionTopView ! = null ) {
142- ImmersionBar . setTitleBar ( this , immersionTopView ) ;
128+ if (immersionTopView = = null ) {
129+ return ;
143130 }
144- }
131+ immersionTopView .setPadding (immersionTopView .getPaddingLeft (), statusBarHeight ,
132+ immersionTopView .getPaddingRight (), immersionTopView .getPaddingBottom ());
133+ });
134+ mNavigationBarHeightLiveData .observe (this , navigationBarHeight -> {
135+ if (navigationBarHeight == null ) {
136+ return ;
137+ }
138+ View immersionBottomView = getImmersionBottomView ();
139+ if (immersionBottomView == null ) {
140+ return ;
141+ }
142+ immersionBottomView .setPadding (immersionBottomView .getPaddingLeft (), immersionBottomView .getPaddingTop (),
143+ immersionBottomView .getPaddingRight (), navigationBarHeight );
144+ });
145+ ViewCompat .setOnApplyWindowInsetsListener (getWindow ().getDecorView (), new OnApplyWindowInsetsListener () {
146+
147+ @ NonNull
148+ @ Override
149+ public WindowInsetsCompat onApplyWindowInsets (@ NonNull View v , @ NonNull WindowInsetsCompat insets ) {
150+ Insets windowInsets = getWindowInsets (insets );
151+ Integer statusBarHeight = mStatusBarHeightLiveData .getValue ();
152+ if (statusBarHeight == null || statusBarHeight != windowInsets .top ) {
153+ mStatusBarHeightLiveData .postValue (windowInsets .top );
154+ }
155+ Integer navigationBarHeight = mNavigationBarHeightLiveData .getValue ();
156+ if (navigationBarHeight == null || navigationBarHeight != windowInsets .bottom ) {
157+ mNavigationBarHeightLiveData .postValue (windowInsets .bottom );
158+ }
159+ return insets ;
160+ }
161+ });
162+ }
163+
164+ /**
165+ * 获取系统栏的高度
166+ */
167+ public Insets getWindowInsets (@ NonNull WindowInsetsCompat insets ) {
168+ return insets .getInsets (WindowInsetsCompat .Type .systemBars ());
169+ }
170+
171+ /**
172+ * 监听状态栏高度变化
173+ */
174+ public void observeStatusBarHeight (@ NonNull Observer <Integer > observer ) {
175+ observeStatusBarHeight (this , observer );
176+ }
177+
178+ public void observeStatusBarHeight (@ NonNull LifecycleOwner lifecycleOwner , @ NonNull Observer <Integer > observer ) {
179+ mStatusBarHeightLiveData .observe (lifecycleOwner , observer );
180+ }
181+
182+ /**
183+ * 监听导航栏高度变化
184+ */
185+ public void observeNavigationBarHeight (@ NonNull Observer <Integer > observer ) {
186+ observeNavigationBarHeight (this , observer );
187+ }
188+
189+ public void observeNavigationBarHeight (@ NonNull LifecycleOwner lifecycleOwner , @ NonNull Observer <Integer > observer ) {
190+ mNavigationBarHeightLiveData .observe (lifecycleOwner , observer );
145191 }
146192
147193 /**
@@ -155,6 +201,15 @@ protected boolean isStatusBarEnabled() {
155201 * 状态栏字体深色模式
156202 */
157203 protected boolean isStatusBarDarkFont () {
204+ // 返回 true 表示黑色字体
205+ return true ;
206+ }
207+
208+ /**
209+ * 获取导航栏图标颜色
210+ */
211+ protected boolean isNavigationBarDarkIcon () {
212+ // 返回 true 表示黑色图标
158213 return true ;
159214 }
160215
@@ -174,20 +229,13 @@ public ImmersionBar getStatusBarConfig() {
174229 */
175230 @ NonNull
176231 protected ImmersionBar createStatusBarConfig () {
177- ImmersionBar immersionBar = ImmersionBar .with (this )
178- // 默认状态栏字体颜色为黑色
232+ return ImmersionBar .with (this )
233+ // 设置状态栏字体的颜色
179234 .statusBarDarkFont (isStatusBarDarkFont ())
180- // 状态栏字体和导航栏内容自动变色,必须指定状态栏颜色和导航栏颜色才可以自动变色
181- .autoDarkModeEnable (true , 0.2f );
182- // 适配 Android 15 EdgeToEdge 特性
183- if (AndroidVersion .isAndroid15 ()) {
184235 // 设置透明的导航栏
185- immersionBar .transparentNavigationBar ();
186- } else {
187- // 指定导航栏背景颜色
188- immersionBar .navigationBarColor (R .color .white );
189- }
190- return immersionBar ;
236+ .transparentNavigationBar ()
237+ // 设置导航栏图标的颜色
238+ .navigationBarDarkIcon (isNavigationBarDarkIcon ());
191239 }
192240
193241 /**
0 commit comments