android - 将ColorFilter设置为按钮而不改变笔画

标签 android android-drawable android-button

我有一个 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/

相关文章:

java - 得到此异常 : java. lang.IllegalArgumentException : No Retrofit annotation found. (参数 #2)

android - 使用Gradle任务将productFlavor值写入文件

android - android studio 中的多个 Github 帐户

android - 我在哪里可以找到默认的 android xml 绘图?

android - 从 android 中的字符串数组引用可绘制对象

android - 在 Button 中动画化 Drawable 变化

Android maxLines 和 minLines 属性在 XML 中不起作用

android - Material 按钮中定义的样式与 AppTheme 不兼容

android - 如何使用 style.xml 文件中的样式(以及所有其他样式)创建无边框按钮

android - 在圆圈内创建右箭头