已经有很多类似的问题了,比如
this one
,但答案不再适用了。
以下是我创建按钮的方式:
<Button
android:id="@+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
以下是我更改颜色的方式:
myButton.getBackground().setColorFilter(lightSalmon, PorterDuff.Mode.SRC);
到目前为止,一切顺利。现在我想把它改回默认值。这就是我迄今为止所尝试的:
myButton.getBackground().clearColorFilter();
使按钮背景为白色,而不是默认的灰色。等同于:
myButton.getBackground().setColorFilter(null);
这将完全改变按钮的外观:
myButton.setBackgroundResource(android.R.drawable.btn_default);
我发现
setTint()
在API中,但我没有尝试过,因为它需要API级别21(我在15)。是否有向后兼容的方法来
setTint()
?
试图以编程方式记住默认颜色也不起作用,因为
getColorFilter()
还需要API级别21。(
setColorFilter()
另一方面,从API级别1)开始就存在。
我能想到的另一个选择是只应用默认的灰色(如果我能确定它的值)。但我担心这可能是有偏见的:默认的灰色可能在其他手机上有所不同,有其他安卓版本,其他主题和风格。。。
我还有其他选择吗?