我有一个 xml,其中按钮的背景可绘制为:
<shape android:shape="rectangle">
<corners android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"/>
<stroke android:width="1dp" android:color="@color/black"/>
</shape>
我只想以编程方式更改按钮背景的颜色。 所以我尝试了这个,
button.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.red), PorterDuff.Mode.SRC_ATOP);
但是,当我 setColorFIlter() 时,我根本看不到笔画。 有任何输入吗?
最佳答案
这对我有用。
GradientDrawable buttonBackground = (GradientDrawable)button.getBackground();
buttonBackground.setColor(color);
关于android - 将ColorFilter设置为按钮而不改变笔画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37699886/