gpt4 book ai didi

java - 调用 setStatusBarColor ANDROID

转载 作者:太空狗 更新时间:2023-10-29 16:01:51 24 4
gpt4 key购买 nike

如何在单击按钮时调用 setStatusBarColor?我有事件监听器代码,但不确定如何调用此方法。我正在尝试更改按钮单击时的状态栏颜色。

这是我的代码:

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();
}
});
}

最佳答案

将您的方法调用更改为此

Activity 中

public void onClick(View view) {
setStatusBarColor(this, Color.parseColor("#4CAF50"));
}

在 fragment 中:

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);
}
}

关于java - 调用 setStatusBarColor ANDROID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com