如何以编程方式添加按钮可绘制颜色

时间:2012-12-28 07:57:33

标签: android

  

可能重复:
  Android - How to programmatically set button color

我在程序中动态添加了表格行,并添加了按钮,但按钮的颜色没有变化。我添加了一个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));

6 个答案:

答案 0 :(得分:1)

有效..

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

如果您使用hexcode

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

答案 1 :(得分:0)

尝试此操作(如果要将图片添加到背景中):

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

答案 2 :(得分:0)

如果您想尝试,那么您可以以编程方式提供颜色和hexcode,如下所述。

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

(vatsalshah.co.in)

答案 3 :(得分:0)

setBackgroundColor采用颜色值(argb,int),R.drawable.redbtn是资源ID(int) 它永远不会起作用。

如果你想从资源中应用背景并且你有它的ID,你应该使用setBackgroundResource(int resid)

答案 4 :(得分:0)

使用此

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn);

代替,

Deletecomponentbtn.setBackgroundColor(R.drawable.redbtn);

感谢。

答案 5 :(得分:0)

使用

Deletecomponentbtn.setBackgroundResource(R.drawable.redbtn);

并且可以使用不同的按钮状态更正redbtn.xml。

相关问题