更改按钮的背景颜色

时间:2014-06-14 15:15:19

标签: android

我想更改此按钮的背景:

              <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_alignParentRight="true"
               android:layout_below="@+id/button2"
               android:minHeight="106dp"
               android:text="Button" />

我该怎么做?

4 个答案:

答案 0 :(得分:0)

创建一个buttoncolor.xml文件并将其放入:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#FF0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#FF0000" />
<item android:color="#ffffff" />

然后在你的按钮属性中:

android:background="@drawable/buttoncolor"

编程:

button.setBackgroundColor(Color.WHITE);

答案 1 :(得分:0)

对该按钮使用android:background =“”属性。 例如,您可以在上面一行的“”中使用#22CCDD

答案 2 :(得分:0)

使用android:background=""值可以是彩色或可绘制的

答案 3 :(得分:0)

使用下面的drawble文件,假设是button_bg.xml

<shape android:shape="rectangle">
 <solid android:color="@android:color/transparent"/> --> change any color here accorring  to your requirement,and this will the background of your button.
     <corners android:bottomLeftRadius="8dp"  --> use this if you want to make button corners rounded.
              android:bottomRightRadius="8dp" 
              android:topLeftRadius="8dp" 
              android:topRightRadius="8dp"/>

 <stroke android:width="1dp" --> use this if you want button border.
         android:color="#9c9a9b"/> --> change any color here accorring to your requirement

这是您的按钮代码

Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentRight="true"
           android:layout_below="@+id/button2"
           android:minHeight="106dp"
           android:background="@drawable/button_bg" -- > set the above file like this.
           android:text="Button" />

或者很简单,您可以制作图像并将其放入可绘制文件夹中,并可以设置为按钮背景,如:

android:background="@drawable/YOUR_IMAGE_NAME"