先看效果图
原来的效果图是这样的
我是这样解决的
WindowInsetsController controller = getWindow().getInsetsController();
if (controller != null) {
// 设置虚拟按键的透明度,0为全透明,255为不透明
controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS, WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
}
把这个加上就会就会出现第一张图片的效果,下面的代码效果对全面屏也有一样的效果,
我把完整代码也贴出来
public class MainActivity_main_page extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
//将每个菜单ID作为一组ID传递,因为每个
try {
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
// View customActionBar = LayoutInflater.from(this).inflate(R.layout.zdyssk,
// new LinearLayout(this), false);
getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
mw();
StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 50);
} catch (Exception e) {
e.printStackTrace();
}
}
public void mw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 获取WindowInsetsController
WindowInsetsController controller = getWindow().getInsetsController();
if (controller != null) {
// 设置虚拟按键的透明度,0为全透明,255为不透明
controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS, WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// 对于Android Pie (API 28) 及以下版本,可以尝试调整亮度
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_LOW_PROFILE) == 0) {
// 当系统UI变为非低亮度模式时,调整亮度
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
} else {
// 当系统UI变为低亮度模式时,恢复亮度
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
});
}
}
最主要的就是就是这两个方法
getWindow().setNavigationBarColor(getResources().getColor(R.color.white));
controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS, WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
第一个是让我们的底部颜色变成白色,第二个就可以让我们的底部变成透明和那三个按键变成灰色的,里面的第一个也就是我们使用的样子,第二个是建议不要使用,他会把那三个按键变成白色的
WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
里面
StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 50);这个方法不避理会,对虚拟按键没用
/**
* 使状态栏变得不透明,具有纯色深色背景和浅色前景。
* @hide
*/
int APPEARANCE_OPAQUE_STATUS_BARS = 1;
/**
* 使导航栏变得不透明,具有纯色深色背景和浅色前景
* @hide
*/
int APPEARANCE_OPAQUE_NAVIGATION_BARS = 1 << 1;
/**
* 在不更改条形图布局的情况下,使系统栏上的项目变得不那么明显。
* @hide
*/
int APPEARANCE_LOW_PROFILE_BARS = 1 << 2;
/**
* 更改灯光状态栏的前景色,以便可以清楚地读取灯状态栏上的项目。
* clearly.
*/
int APPEARANCE_LIGHT_STATUS_BARS = 1 << 3;
/**
* 更改灯光导航栏的前景色,以便可以清楚地读取栏上的项目。
* read clearly.
*/
int APPEARANCE_LIGHT_NAVIGATION_BARS = 1 << 4;
/**
* 使状态栏半透明,背景为深色,前景为浅色。
* @hide
*/
int APPEARANCE_SEMI_TRANSPARENT_STATUS_BARS = 1 << 5;
/**
* 使导航栏半透明,背景为深色,前景为浅色。
* @hide
*/
int APPEARANCE_SEMI_TRANSPARENT_NAVIGATION_BARS = 1 << 6;
这个是Android源代码里面的解释具体也什么作用,自己可以慢慢选择尝试