【问题标题】:Calling setStatusBarColor ANDROID调用 setStatusBarColor ANDROID
【发布时间】:2015-05-16 01:42:37
【问题描述】:

如何在 buttonclick 上调用 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();
            }
        });
    }

【问题讨论】:

  • 你能解释一下什么不起作用吗?
  • 至少在你的按钮上显示监听代码。
  • 道歉@ci_。我正在尝试使用 setStatusBarColor(); 调用它
  • @HugoGresse 见上文
  • @user1353517 好像你没有编辑你的问题,请清楚地解释发生了什么

标签: java android statusbar buttonclick


【解决方案1】:

将您的方法调用更改为此

活动中

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

【讨论】:

  • 我尝试从方法中删除参数,但我遇到“无法解析符号'活动'”和“无法解析符号状态栏颜色”
  • 我也尝试了第二种方法,但我遇到了无法解析方法 getActivity() 而第一种方法说它无法应用
  • 它不能被静态上下文引用。 if 语句中的 statusBarColor 也会引发无法解析符号错误:/
  • 我仍然面临状态栏颜色错误。如果我将其更改为 public void,则将其更改为 getWindow() 即可。
  • 你能从你的方法中删除静态 public void setStatusBarColor()
猜你喜欢
  • 2015-08-22
  • 2019-04-18
  • 2022-01-03
  • 2017-05-10
  • 2011-06-06
  • 1970-01-01
  • 2014-06-29
  • 2019-12-13
  • 2017-08-07
相关资源
最近更新 更多