繁体   English   中英

Android Studio 使用自定义绘图更改按钮背景颜色

[英]Android Studio change button background color with custom drawable

我在 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>

在一个按钮上,我想以编程方式将按钮的颜色更改为另一种颜色,同时保持它从自定义可绘制对象获得的形状。 这可能吗?

如果您不想在 xml 文件夹中使用普通背景色:

    android:backgroundTint="yourcolor"

在您的 java 代码中使用此代码:

    yourbutton.setBackgroundTintList(this.getResources().getColorStateList(R.color.colorname));
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM