简体   繁体   中英

Android: How to change the color of a button?

我正在使用Eclipse ADT,并且我需要知道如何使用xml将表单窗口小部件按钮的默认颜色从默认的灰色更改为其他颜色。

Or you can use 9 patch images.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="@drawable/patchdisable" android:state_enabled="false"/>

正常追问残

And in Xml

        <Button
        android:id="@+id/button_register"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/patch"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="REGISTER"
        android:textColor="#ffffff"
        android:textStyle="bold" />

Using code in you activity class:

Button btn = (Button)findViewById(R.id.button);
btn.setBackgroundColor(Color.BLUE);
btn.setBackgroundColor(Color.rgb(0, 0, 255));
btn.setBackgroundColor(Color.parseColor("blue"));

Doing it straight from your XML:

Add this property to your Button in your XML file:

android:background="#800080"

#800080 is purple.

In your xml, you can set the background color by using android:background="#FF0000"

<Button
        android:id="@+id/loadimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load Image" 
        android:background="#FFFF00"/>

you can set any background color of your choice...:)

Here is answer my friend

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/grey"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM