我想使用selector-xml文件更改按鈕的背景顏色。我的方法基本上是從例子中,一個在底部此頁:http://developer.android.com/guide/topics/resources/color-list-resource.html具有不同背景顏色的Android按鈕
我有一個RES /顏色/ button_text.xml看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
和我的佈局包含以下代碼:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
**android:background="@color/button_text"** />
(**只是沒有告訴你,我使用Android:背景,而不是機器人的:文本顏色)
該代碼崩潰。它說:「二進制XML文件行#4標籤需要'drawable'屬性或子標籤定義drawable。但是如果我嘗試使用android:textColor,如上面的鏈接中所述,它工作正常,所以它必須是背景問題。如果不需要創建9patch-png(基本上我只需要一個「可點擊」的矩形,所以我使用帶有彩色背景的按鈕)
康斯坦丁的回答有一個錯字 - 行'項目的android:顏色=」 @顏色/黑色「'..........我建議它應該讀取項目android:drawable =」@ color/black「........... – 2011-02-09 12:22:59
http:// stackoverflow。 com/a/15880688/1352919 – Faakhir 2013-04-08 13:45:43