2013-07-17 31 views
3

我:安卓:改變按鈕的顏色編程方式定義繪製

public Button[] getButtons(int buttonCount, List<String> buttonHeaders, Context activity) { 
    Button[] result = new Button[buttonCount]; 

    for (int i = 0; i < buttonCount; i++) { 

     result[i] = new Button(activity); 

     result[i].setId(i); 
     result[i].setText(buttonHeaders.get(i)); 
     result[i].setBackgroundResource(R.drawable.buttons_shape); 
     result[i].setBackgroundColor(randomColor(activity)); 
     result[i].setTextColor(activity.getResources().getColor(R.color.white)); 
     result[i].setWidth(360); 
     result[i].setHeight(100); 
    } 
    return result; 
} 

但在我的繪製是:

<solid android:color="@color/white"/> 
    <corners 
     android:bottomRightRadius="10dp" 
     android:bottomLeftRadius="10dp" 
     android:topLeftRadius="10dp" 
     android:topRightRadius="10dp"/> 

,但我想定義程序是什麼顏色,因爲顏色是隨機,當我改變背景是回到默認設計(PS。對不起,英文:P)

回答

0

你不能做這兩個..你可以做view.setBackgroundDrawable(drawable);或view.setBackGroundColor(color);

+0

是的,當我使用setBackgroundDrawable(drawable)設置我自己的風格,但後來我想要改變顏色與setBackgroundColor它返回默認樣式,並設置顏色沒有圓角 – Maury

+0

這是因爲風格屬於drawable而不是你的所以,調用setBackGroundColor刪除backgroundDrawable。 –

+0

那麼如何解決這個問題? – Maury

相關問題