背景:android api 35,activity设置EdgeToEdge.enable((ComponentActivity) this)前提下
一、设置导航栏与状态栏颜色
设置的状态栏颜色,只需要设置fitsSystemWindows跟setOnApplyWindowInsetsListener
xml设置:
代码:
getWindow().setNavigationBarContrastEnforced(false);
ViewCompat.setOnApplyWindowInsetsListener(mMyGameRecycler, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(insets.left, v.getPaddingTop(), insets.right, insets.bottom);
return WindowInsetsCompat.CONSUMED;
});
ViewCompat.setOnApplyWindowInsetsListener(mMyGameRecycler, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(insets.left, v.getPaddingTop(), insets.right, insets.bottom);
return WindowInsetsCompat.CONSUMED;
});
二,设置导航栏自定义颜色半透明
布局activity_my_game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<View
android:id="@+id/view_tool"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/white" />
<include layout="@layout/toolbar_custom" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/swipe_target"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
<com.zhiyicx.baseproject.widget.EmptyView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
代码层面:
//设置导航栏与状态栏图标颜色
StatusBarUtils.setStatusBarLightMode(this.getWindow(), true);
StatusBarUtils.setNavigationBarLightMode(this.getWindow(), true);
//设置导航栏背景颜色与透明度,注意:如果布局fitsSystemWindows 为true时则不生效
int bgcolor = Color.argb(0xe6, 0xFF, 0xFF, 0xFF);
getWindow().setNavigationBarColor(bgcolor);
//设置列表布局滑动时在导航栏背后,滑动到底时在导航栏顶部
mMyGameRecycler.setClipToPadding(false);
ViewCompat.setOnApplyWindowInsetsListener(mMyGameRecycler, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(insets.left, v.getPaddingTop(), insets.right, insets.bottom);
return WindowInsetsCompat.CONSUMED;
});
注意:
- 设置导航栏颜色时需要添加getWindow().setNavigationBarContrastEnforced(false),但是设置半透明时需要移除该行代码
- 当设置fitsSystemWindows 时setNavigationBarColor会不生效