Skip to content

Commit 596faf0

Browse files
xhhuxhhu
authored andcommitted
update:适配垃圾安卓的垃圾15
1 parent f9aa9a3 commit 596faf0

3 files changed

Lines changed: 62 additions & 12 deletions

File tree

translucentparent/src/main/java/com/cy/translucentparent/NavigationBarView.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.util.AttributeSet;
55
import android.view.View;
66

7+
import androidx.annotation.NonNull;
78
import androidx.core.view.ViewCompat;
89
import androidx.core.view.WindowInsetsCompat;
910

@@ -13,16 +14,28 @@
1314
*/
1415

1516
public class NavigationBarView extends View {
17+
private int height;
18+
1619
public NavigationBarView(Context context) {
1720
this(context,null);
1821
}
1922

2023
public NavigationBarView(Context context, AttributeSet attrs) {
2124
super(context, attrs);
25+
height = StaNavUtils.getNavigationBarHeight(this);
26+
ViewCompat.setOnApplyWindowInsetsListener(this, new androidx.core.view.OnApplyWindowInsetsListener() {
27+
@NonNull
28+
@Override
29+
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
30+
height = StaNavUtils.getNavigationBarHeight(v);
31+
requestLayout();
32+
return insets;
33+
}
34+
});
2235
}
2336

2437
@Override
2538
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
26-
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(StaNavUtils.getNavigationBarHeight(this), MeasureSpec.EXACTLY));
39+
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
2740
}
2841
}

translucentparent/src/main/java/com/cy/translucentparent/StaNavUtils.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public static void edgeToEdge(ComponentActivity componentActivity) {
4848

4949
StatusBarView statusBarView = new StatusBarView(componentActivity);
5050
statusBarView.setId(ID_STATUSBAR);
51-
// statusBarView.setBackgroundColor(Color.WHITE);
52-
// setAppearanceLightStatusBars(componentActivity, true);
5351
setStatusBarColor(componentActivity, Color.WHITE);
5452
linearLayout.addView(statusBarView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
5553

@@ -59,11 +57,9 @@ public static void edgeToEdge(ComponentActivity componentActivity) {
5957

6058
NavigationBarView navigationBarView = new NavigationBarView(componentActivity);
6159
navigationBarView.setId(ID_NAVIGATIONBAR);
62-
//这样会是半透明的,不好看
63-
// navigationBarView.setBackgroundColor(callback.setNavigationBarColor());
64-
// setAppearanceLightNavigationBars(componentActivity, isLightColor(callback.setNavigationBarColor()));
6560
setNavigationBarColor(componentActivity, Color.WHITE);
6661
linearLayout.addView(navigationBarView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
62+
6763
decorView.addView(linearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
6864
}
6965

@@ -248,15 +244,32 @@ public static void setStatusBarColor(Activity activity, int color) {
248244
* at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:879)
249245
* at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:667)
250246
*/
251-
View view_statusbar = activity.findViewById(ID_STATUSBAR);
252-
if (view_statusbar != null) view_statusbar.setBackgroundColor(color);
247+
View view = activity.findViewById(ID_STATUSBAR);
248+
view.setBackgroundColor(color);
253249
} catch (Exception e) {
254250

255251
}
256252
boolean isLightColor = isLightColor(color);
257253
setAppearanceLightStatusBars(activity, isLightColor);
258254
}
259255

256+
public static void setNavigationBarColor(Activity activity, int color) {
257+
try {
258+
/**
259+
* Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.ContentFrameLayout.setDecorPadding(int, int, int, int)' on a null object reference
260+
* at androidx.appcompat.app.AppCompatDelegateImpl.applyFixedSizeWindow(AppCompatDelegateImpl.java:1085)
261+
* at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:879)
262+
* at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:667)
263+
*/
264+
View view = activity.findViewById(ID_NAVIGATIONBAR);
265+
view.setBackgroundColor(color);
266+
} catch (Exception e) {
267+
268+
}
269+
boolean isLightColor = isLightColor(color);
270+
setAppearanceLightNavigationBars(activity, isLightColor);
271+
}
272+
260273
public static void setStatusBarColorOld(Activity activity, int color) {
261274
Window window = activity.getWindow();
262275
//去除statusbar不填充的标志
@@ -277,11 +290,10 @@ public static void setStatusBarColorOld(Activity activity, int color) {
277290

278291
/**
279292
* 设置导航栏颜色,和setNavigationBarTransparent互斥,要么选择自定义导航栏颜色,要么选择导航栏全透明
280-
*
281293
* @param activity
282294
* @param color
283295
*/
284-
public static void setNavigationBarColor(Activity activity, int color) {
296+
public static void setNavigationBarColorOld(Activity activity, int color) {
285297
Window window = activity.getWindow();
286298
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
287299
int ui = window.getDecorView().getSystemUiVisibility();
@@ -302,6 +314,12 @@ public static int getStatusBarHeight(Activity activity) {
302314
return getStatusBarHeight(activity.getWindow().getDecorView());
303315
}
304316

317+
/**
318+
* 注意:安卓15及以上不准确,必须 setOnApplyWindowInsetsListener 监控才能获取正确的高度
319+
*
320+
* @param view
321+
* @return
322+
*/
305323
public static int getStatusBarHeight(View view) {
306324
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(view);
307325
int h = 0;
@@ -314,11 +332,17 @@ public static int getNavigationBarHeight(Activity activity) {
314332
return getNavigationBarHeight(activity.getWindow().getDecorView());
315333
}
316334

335+
/**
336+
* 注意:安卓15及以上不准确,必须 setOnApplyWindowInsetsListener 监控才能获取正确的高度
337+
*
338+
* @param view
339+
* @return
340+
*/
317341
public static int getNavigationBarHeight(View view) {
318342
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(view);
319343
int h = 0;
320344
if (insets != null)
321-
h= insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.navigationBars()).bottom;
345+
h = insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.navigationBars()).bottom;
322346
return Math.max(h, getNavigationBarHeightLegacy(view.getContext()));
323347
}
324348

translucentparent/src/main/java/com/cy/translucentparent/StatusBarView.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import android.util.Log;
66
import android.view.View;
77

8+
import androidx.annotation.NonNull;
89
import androidx.core.graphics.Insets;
10+
import androidx.core.view.OnApplyWindowInsetsListener;
911
import androidx.core.view.ViewCompat;
1012
import androidx.core.view.WindowInsetsCompat;
1113

@@ -15,16 +17,27 @@
1517
*/
1618

1719
public class StatusBarView extends View {
20+
private int height;
1821
public StatusBarView(Context context) {
1922
this(context, null);
2023
}
2124

2225
public StatusBarView(Context context, AttributeSet attrs) {
2326
super(context, attrs);
27+
height = StaNavUtils.getStatusBarHeight(this);
28+
ViewCompat.setOnApplyWindowInsetsListener(this, new androidx.core.view.OnApplyWindowInsetsListener() {
29+
@NonNull
30+
@Override
31+
public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowInsetsCompat insets) {
32+
height = StaNavUtils.getStatusBarHeight(v);
33+
requestLayout();
34+
return insets;
35+
}
36+
});
2437
}
2538

2639
@Override
2740
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
28-
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(StaNavUtils.getStatusBarHeight(this), MeasureSpec.EXACTLY));
41+
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
2942
}
3043
}

0 commit comments

Comments
 (0)