所以我遇到了以下问题:我目前正在开发一个传感器应用程序,我想检查传感器是否可用。如果不是,我想将按钮的颜色(它启动显示传感器值的活动(更改为灰色。遗憾的是,我不能仅仅更改按钮的背景颜色,因为我使用的是Markushi的Circle button Library。这个按钮看起来像这样:
<at.markushi.ui.CircleButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/gyroscope"
app:cb_color="@color/colorMain"
app:cb_pressedRingWidth="8dip"
android:id="@+id/gyroscope"
android:layout_alignLeft="@+id/temperature"
android:layout_alignStart="@+id/temperature"
android:layout_below="@+id/title"/>
正如您所看到的,该属性定义了颜色。
app:cb_color="@color/colorMain"
我的问题是:如何用这种方法程序化地改变这种颜色?
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
}
}
编辑:setColor实际上不起作用。还有这个边界。(见图(另外:我使用#616161作为新颜色。
第2版:发现,边框是由按下按钮时变大的透明圆圈引起的。所以基本上去掉这个圆圈就可以了。我试着自己找到答案:(
CircleButton button;
button = (Button)findViewById(R.id.buttonId);
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
button.setColor(Color.parse("#000000"));
}
}
如果您使用的是普通button
,则
button.setBackgroundColor(ContextCompat.getColor(context,R.color.colorAccent));
你可以使用
1( Color.parse("#000000")
2( ContextCompat.getColor(context,R.color.yourColor)
您可以使用circleButton的setColor()
方法,正如您从这里的源代码中看到的那样。
所以,基本上,你需要做的是使用活动的findViewById
方法来获得对圆圈按钮的引用。然后
public void testSensors() {
if (testManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE) == null) {
myCircleButton.setColor(Color.parse("#000000"));
}
}
此外,如果你看到按下的环,那么这意味着按钮的状态在某种程度上处于按下状态,所以尝试使用setPressed(false)
方法将其设置为false。
要回答这个问题(如果有人有同样的问题(:可以将动画设置为100。这以某种方式禁用了圆。出于某种原因,按钮的颜色现在有了固定的颜色(深灰色(,但闪烁——这对我来说很好,我不想在桌子下面哭,我只是顺其自然,继续我的项目。
mCircleButton.setAnimationProgress(100);
非常感谢大家的帮助!:(