单击Android时,在工具栏中更改图标的颜色

发布于 2025-01-25 18:41:37 字数 200 浏览 2 评论 0原文

当我单击时,我想将工具栏中的图标喜欢的颜色更改为红色。我该怎么做

Screen demo

I want to change color of icon favorite in toolbar to red when I click it. How can I do that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2025-02-01 18:41:37

这就是这样做的方法。您可以更改图标颜色,但要执行的功能仅适用于API 26+。为了使其在API 26以下的工作,您需要完全更改图标,因为没有其他方法可以更改颜色。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == R.id.delete_icon) {
        isSelected = !isSelected
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Toggle state on devices with API 26+
            if (isSelected) {
                item.iconTintList = ContextCompat.getColorStateList(this, R.color.red)
            } else {
                item.iconTintList = ContextCompat.getColorStateList(this, R.color.white)
            }
        } else {
            // Toggle state on devices with API below 26
            if (isSelected) {
                item.icon =
                    ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_24_red)
            } else {
                item.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_24)
            }
        }
        return true
    }
    return super.onOptionsItemSelected(item)
}

This is the way to do this. You can change the icon colour but the function to do it only works on API 26+. To make it work on below API 26, you need to change the icon altogether as there is no other way to change the colour.

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == R.id.delete_icon) {
        isSelected = !isSelected
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Toggle state on devices with API 26+
            if (isSelected) {
                item.iconTintList = ContextCompat.getColorStateList(this, R.color.red)
            } else {
                item.iconTintList = ContextCompat.getColorStateList(this, R.color.white)
            }
        } else {
            // Toggle state on devices with API below 26
            if (isSelected) {
                item.icon =
                    ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_24_red)
            } else {
                item.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_favorite_24)
            }
        }
        return true
    }
    return super.onOptionsItemSelected(item)
}
请恋爱 2025-02-01 18:41:37

*尝试此代码 -

 btn.setOnClickListener(view -> {
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));

        });

*try this code -

 btn.setOnClickListener(view -> {
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));

        });
治碍 2025-02-01 18:41:37

您可以创建一个全局菜单变量,然后在onCreateOptionsMenu()中初始化它,然后在您的onclick()中使用它。

private Menu menu;

this.menu = menu;

在您的

menu.getItem(0).setIcon(ContextCompat.getDrawable(this, R.drawable.red_heart));

You could create a global Menu variable and initialize it in the onCreateOptionsMenu() and then use it in your onClick().

private Menu menu;

In your onCreateOptionsMenu()

this.menu = menu;

In your button's onClick() method

menu.getItem(0).setIcon(ContextCompat.getDrawable(this, R.drawable.red_heart));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文