[英]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.