Android 设置虚拟按键的透明度针对全面屏和三按钮导航
先看效果图原来的效果图是这样的我是这样解决的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源代码里面的解释具体也什么作用自己可以慢慢选择尝试