简体   繁体   English

Android:如何更改按钮的颜色?

[英]Android: How to change the color of a button?

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

Or you can use 9 patch images. 或者,您可以使用9个补丁图像。

<?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 而在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: 直接从您的XML中进行操作:

Add this property to your Button in your XML file: 将此属性添加到XML文件中的Button中:

android:background="#800080"

#800080 is purple. #800080是紫色的。

In your xml, you can set the background color by using android:background="#FF0000" 在xml中,您可以使用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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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