繁体   English   中英

如何在不更改onclick / onpress颜色的情况下更改按钮背景颜色

[英]How to change button background colour without changing onclick/onpress colors

我想知道这是否有可能,但希望有人能够确认。

我已经用XML创建了一个简单的自定义按钮布局,以处理聚焦/按下和休眠状态。 请参阅底部的代码。 当我使用它创建一个新按钮时,这可以很好地工作。 但是,我希望用户能够通过颜色选择器更改按钮颜色(如果他们不喜欢默认颜色)。 但是,我知道以编程方式更改按钮背景颜色的唯一方法是使用

mybutton.setBackgroundColor(someothercolor);

但是如果执行此操作,它将覆盖所有XML布局代码,并且在按下按钮时会丢失颜色更改。 我猜这是设计使然,因为我实际上是在覆盖整个背景样式,但是我真正想做的是允许用户在未按自定义按钮时更改按钮颜色,但保留其他状态的样式和布局该按钮可能在里面(即按下按钮时会发生什么)。

有任何想法吗?

先感谢您。

纳特

<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/originalbuttoncolor" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:drawable="@color/originalbuttoncolor" />
</selector>

也许您可以考虑以编程方式创建ColorStateList,如下所述: 如何以编程方式创建ColorStateList?

您可以尝试以下方法:
1.删​​除默认颜色<item android:drawable="@color/originalbuttoncolor" />
2.然后:

`StateListDrawable ret = (StateListDrawable) res.getDrawable(R.drawable.btn_selector);
    ret.addState(new int[] {}, new ColorDrawable(your_desire_color));
    mybutton.setBackgroundDrawable(ret);`

您可以创建选择器列表的多个文件,每个文件包含不同的颜色作为默认颜色,并将这些文件链接到颜色选择器,从而节省了选择器的逻辑。

例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/originalbuttoncolor" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:drawable="@color/originalbuttoncolor" />
</selector>

和:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/yellowbuttoncolor" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/someotherbuttoncolor" />
    <item android:drawable="@color/originalbuttoncolor" />
</selector>

编辑:如果您想从用户那里获取颜色,则可能会起作用,如果选择器状态将被以下代码覆盖:

ColorDrawable cd = new ColorDrawable(); // initialize it from the color picker;
StateListDrawable states = (StateListDrawable) mybutton.getBackground();
states.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, cd); // the minus means false value
mybutton.setBackground(states);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM