[英]Android: How to change the color of a button?
我正在使用Eclipse ADT,並且我需要知道如何使用xml將表單窗口小部件按鈕的默認顏色從默認的灰色更改為其他顏色。
或者,您可以使用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"/>
而在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" />
Button btn = (Button)findViewById(R.id.button);
btn.setBackgroundColor(Color.BLUE);
btn.setBackgroundColor(Color.rgb(0, 0, 255));
btn.setBackgroundColor(Color.parseColor("blue"));
將此屬性添加到XML文件中的Button
中:
android:background="#800080"
#800080
是紫色的。
在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"/>
您可以設置您選擇的任何背景顏色... :)
這是我朋友的答案
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/grey"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.