2015-05-15 71 views
2

我試圖以編程方式更改形狀的顏色,但它不起作用。以編程方式在android中更改形狀顏色

這裏是形狀

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="#9F2200"/> 
    <stroke android:width="2dp" android:color="#fff" /> 
</shape> 

這裏是我如何使用它作爲一個按鈕

<Button     
    android:id="@+id/ibtn_EA_ColorPick_new" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:background="@drawable/round_button" 
    /> 

的背景,這裏是我如何改變其顏色

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground(); 
bgShape.setColor(Color.RED); 

但是,當我改變背景顏色時,它會從屏幕上移除按鈕。

+1

@DerGolem:我跟着同樣的問題,但它並沒有爲我工作。 –

回答

2

更改代碼如下

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground(); 
bgShape.mutate() 
bgShape.setColor(Color.RED); 
相關問題