答案 0 :(得分:6)
您可以使用以下两种方法:
getWindow().setStatusBarColor(Color.parseColor("#20111111"));
getWindow().setNavigationBarColor(Color.parseColor("#20111111"));
答案 1 :(得分:1)
除了设置导航栏颜色和状态栏颜色外,还需要做更多的事情,您实际上必须将内容显示在两者的下方。
我在utils类中使用以下方法将其设置在setContentView
之前的活动中
public static void setWindowStatusNav(android.view.Window window, int statusbarColor, int navbarColor) {
int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT || Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT_WATCH) {
window.getAttributes().flags |= flags;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int uiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
window.getDecorView().setSystemUiVisibility(uiVisibility);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.getAttributes().flags &= ~flags;
window.setStatusBarColor(statusbarColor);
window.setNavigationBarColor(navbarColor);
}
}
在活动中使用它:
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int statusBarColor = android.graphics.Color.parseColor("#40FF0000");
int navBarColor = android.graphics.Color.parseColor("#6E00FF00");
MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);
setContentView(R.layout.my_activity);
}
}
要隐藏导航栏和状态栏,请调用此方法:
public static void setWindowStatusNavHidden(android.view.Window window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int uiVisibility = window.getDecorView().getSystemUiVisibility();
uiVisibility |= View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
window.getDecorView().setSystemUiVisibility(uiVisibility);
}
}
使用完成:
public class MyActivity extends AppCompatActivity {
static boolean statusNavVisible = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int statusBarColor = android.graphics.Color.parseColor("#40FF0000");
int navBarColor = android.graphics.Color.parseColor("#6E00FF00");
MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);
setContentView(R.layout.my_activity);
Button btnShowHide = findViewById(R.id.my_button);
btnShowHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (statusNavVisible) {
statusNavVisible = false;
MyUtils.setWindowStatusNavHidden(getWindow());
} else {
statusNavVisible = true;
MyUtils.setWindowStatusNav(getWindow(), statusBarColor, navBarColor);
}
}
});
}
}