1. 程式人生 > >Android 怎麼動態設定shape定義的控制元件的背景色

Android 怎麼動態設定shape定義的控制元件的背景色

首先來一個shape的xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"
    android:useLevel="false">
    <solid
        android:color="#ff0" /> 
    <size android:width="36dp"
        android:height="36dp" /> 
</shape>


在程式碼中可以設定如下:

GradientDrawable p = (GradientDrawable) textview.getBackground();
p.setColor(Color.RED);