[英]Android Studio change button background color with custom drawable
I have a custom drawable using <shape> in xml, with the following code:我在 xml 中有一个使用 <shape> 的自定义可绘制对象,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/blue"/>
<corners
android:radius="150dp"/>
</shape>
on a button, and I would like to programmatically change the button's color to another color while keeping the shape it gets from the custom drawable.在一个按钮上,我想以编程方式将按钮的颜色更改为另一种颜色,同时保持它从自定义可绘制对象获得的形状。 Is this possible?这可能吗?
If you dont want to use the normal backgrounTint in your xml folder:如果您不想在 xml 文件夹中使用普通背景色:
android:backgroundTint="yourcolor"
Use this Code in your java code:在您的 java 代码中使用此代码:
yourbutton.setBackgroundTintList(this.getResources().getColorStateList(R.color.colorname));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.