2012-12-28 36 views
3

可能重複:
Android - How to programmatically set button color如何添加按鈕可繪製彩色programatticaly

我在程序中動態添加錶行和添加按鈕,但按鈕的顏色沒有改變。我添加了一個XML文件來爲按鈕添加顏色redbtn,它在我將它們添加到活動中時工作,但是當我以編程方式添加按鈕樣式時,顏色不會改變。我該怎麼做。

redbtn.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

<item android:state_pressed="true"> 
<shape> 
    <solid android:color="#DF0101" /> 
    <stroke android:width="1dp" android:color="#ef4444" /> 
    <corners android:radius="3dp" /> 
    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
    </shape> 
    </item> 
<item> 
<shape> 
    <gradient android:startColor="#DF0101" android:endColor="#DF0101" android:angle="270" /> 
    <stroke android:width="1dp" android:color="#992f2f" /> 
    <corners android:radius="3dp" /> 
    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> 
    </shape> 
    </item> 


</selector> 

在佈局:

<Button 
       android:id="@+id/btn_spinner_user_search_select" 
       style="?android:attr/buttonStyleSmall" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="25dp" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/redbtn" 
       android:text="@string/btn_delete_user_search_user" /> 

在節目:

TableRow addcomponentrow=new TableRow(Deleteuser.this); 
      addcomponentrow.setId(200); 
      addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

      Button Deletecomponentbtn=new Button(Deleteuser.this); 
      Deletecomponentbtn.setText("Delete"); 
      Deletecomponentbtn.setId(200); 
      Deletecomponentbtn.setPadding(10, 0, 20, 2); 
      Deletecomponentbtn.setBackgroundColor(R.drawable.redbtn); 
      addcomponentrow.addView(Deletecomponentbtn); 

      userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
+0

多數民衆贊成在文本顏色 –

+0

Deletecomponentbtn.setBackgroundColor(getApplicationContext()。getResources()。getColor(R.drawable.redbtn));這是正確的 –

+0

Deletecomponentbtn.setBackgroundColor(getApplicationContext()。getResources()。getColor(R.drawable.redbtn));也不起作用 –

回答

0

試試這個(如果要添加圖片的背景):

Deletecomponentbtn.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.redbtn)); 
+0

nope給我錯誤,當我嘗試這個,要求改變setBackground setBackgroundColor –

+0

Deletecomponentbtn.setBackgroundDrawable(getApplicationContext()。getResources()。getDrawable(R.drawable.redbtn));其工作 –

+0

請檢查編輯的答案。 – Skynet

0

如果你想嘗試,那麼你可以在程序中給出顏色和十六進制代碼,如下所述。

Deletecomponentbtn.setBackgroundColor(Color.parseColor("HexCode")); 

(vatsalshah.co.in)

0

setBackgroundColor需要的顏色值(ARGB,INT),R.drawable.redbtn是資源ID(INT) 這是永遠不會工作。

如果您想從資源運用的背景和你有它的ID,你應該使用setBackgroundResource(INT渣油)

1

它的工作原理..

Deletecomponentbtn.setForeground(Color.GREEN); 
Deletecomponentbtn.setBackground(Color.GREEN); 

如果您使用十六進制編碼

Deletecomponentbtn.setForeground(Color.parseColor("oxff00ff00")); 
Deletecomponentbtn.setBackground(Color.parseColor("oxff00ff00")); 
0

使用此,

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn); 

相反的,

Deletecomponentbtn.setBackgroundColor(R.drawable.redbtn); 

感謝。

0

使用

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn); 

,可以糾正按鈕的不同狀態redbtn.xml。