Java 调用setStatusBarColor ANDROID
如何在按钮上调用setStatusBarColor?我有事件侦听器代码,但不确定如何调用此方法。我正在尝试更改按钮单击时状态栏的颜色 这是我的密码:Java 调用setStatusBarColor ANDROID,java,android,statusbar,buttonclick,Java,Android,Statusbar,Buttonclick,如何在按钮上调用setStatusBarColor?我有事件侦听器代码,但不确定如何调用此方法。我正在尝试更改按钮单击时状态栏的颜色 这是我的密码: public static void setStatusBarColor(Activity activity, int statusBarColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
public static void setStatusBarColor(Activity activity, int statusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
Window window = activity.getWindow();
if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(Color.parseColor("#4CAF50"));
}
}
这是我的按钮侦听器
public void addButtonListener() {
Button = (Button) findViewById(R.id.Button);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setStatusBarColor();
}
});
}
将方法调用更改为此 活动中
public void onClick(View view) {
setStatusBarColor(this, Color.parseColor("#4CAF50"));
}
在片段中:
public void onClick(View view) {
setStatusBarColor(getActivity() , Color.parseColor("#4CAF50"));
}
或从方法中删除参数
public void onClick(View view) {
setStatusBarColor();
}
public static void setStatusBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
//change here
Window window = activity.getWindow();
// By -->>>>> Window window = getWindow();
//or by this if call in Fragment
// -->>>>> Window window = getActivity().getWindow();
int statusBarColor = Color.parseColor("#4CAF50");
if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(statusBarColor);
}
}
你能解释一下什么不起作用吗?至少在你的按钮上显示侦听器代码。道歉@ci\u。我试图用setStatusBarColor()调用它@雨果女above@user1353517似乎您没有编辑您的问题,请清楚地解释我试图从方法中删除参数的情况,但我面临“无法解析符号‘活动’”和“无法解析符号状态”的问题。BarColori也尝试了第二种方法,我面临无法解析方法getActivity()第一种方式表示它不能被appliedit从静态上下文引用。if语句中的statusBarColor也会抛出一个无法解决的符号错误:/I仍然面临状态栏颜色错误。将其更改为getWindow()如果我将其更改为public void,则可以使用。能否从方法public void setStatusBarColor()中删除static