2014-03-24 38 views
0

編輯我做了建議的更改,但仍然收到相同的錯誤,如下所示。Android:在xml中聲明顏色

我有一個佈局,我試圖設置背景顏色動態響應點擊等。

這是佈局的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_pare" 
    android:gravity="center_vertical" 
    android:background="@drawable/custom_style" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView"/> 
</RelativeLayout> 

而這正是我想聲明的顏色,存儲在可繪製/ custom_style.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:color="#ff0000" android:state_selected="true"/> 
    <item android:color="#ffffff" android:state_pressed="true" /> 
    <item android:color="#e3e3e3"/> 
</selector> 

問題是當我嘗試運行此應用程序崩潰,出現以下錯誤:

03-24 16:29:50.540: E/AndroidRuntime(17643): FATAL EXCEPTION: main 
03-24 16:29:50.540: E/AndroidRuntime(17643): android.view.InflateException: Binary XML     file line #7: Error inflating class <unknown> 
03-24 16:29:50.540: E/AndroidRuntime(17643): at android.view.LayoutInflater.createView(LayoutInflater.java:626) 
03-24 16:29:50.540: E/AndroidRuntime(17643): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)... 
+0

看到我的答案有錯誤,當你聲明你的顏色。 – Fllo

+1

在你的XML中,你已經設置了layout_height =「fill_pare」,如果你把fill_parent(或者更好的match_parent)仍然存在錯誤?如果錯誤仍然存​​在,您可以發佈崩潰活動的onCreate方法的代碼? – jaumebd

+0

@jaumebd我認爲你只是找到解決方案! ^^ – Fllo

回答

3

這不是color繪製(.XML),它應該是:

android:background="@drawable/custom_style" 

你應該把你的custom_style.xmldrawable文件夾:
(右鍵單擊文件夾res並創建一個名爲drawable的文件夾)

enter image description here

還改變你的selector如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@color/color_one" android:state_selected="true"/> 
    <item android:drawable="@color/color_two" android:state_pressed="true" /> 
    <item android:drawable="@color/color_three"/> 
</selector> 

進入/ RES /值創建一個名爲colors.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="color_one" >#ff0000</color> 
    <color name="color_two" >#ffffff</color> 
    <color name="color_three" >#e3e3e3</color> 
</resources> 

最後,@color/color_name引用在您的項目的選擇你的顏色。

從參考:

Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").


由於@jaumebd(鷹眼 ^^)說,你的問題是這條線android:layout_height="fill_pare",這是android:layout_height="fill_parent"

這應該工作。

+0

第二個XML縮減了我五個「顏色類型不允許」錯誤。 – spacitron

+0

太棒了,終於id不會崩潰。儘管如此,仍然沒有做我想做的事。 – spacitron

+0

啊..很高興閱讀!祝你好運。 – Fllo

0

變化

android:background="@color/custom_style" 

android:background="@drawable/custom_style"` 

和地點custom_style.xml文件夾 「繪製」 在

+0

那麼我得到完全相同的錯誤。 – spacitron

+0

custom_style在可繪製的文件夾(不是值)? – MichelRobico

+0

是的。它給了我同樣的錯誤。這就是爲什麼我把它移到顏色(對不起,不是值)的原因。 – spacitron

0

你應該把custom_style.xml到您繪製的文件夾。

更改以下在XML文件中的代碼:

android:background="@color/custom_style" 

android:background="@drawable/custom_style" 
0

首先把你的custom_style在res /可繪製文件夾(如果不是在你的項目中存在,創建它) 。

在您的代碼中,您嘗試使用可繪製的xml作爲顏色,這是錯誤。

要被拉伸設置爲背景正確,您必須使用:

android:background="@drawable/custom_style" 
0

custom_style.xml應在繪製文件夾。因此,您的佈局XML文件將更改爲

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_pare" 
    android:gravity="center_vertical" 
    android:background="@drawable/custom_style" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView"/> 
</RelativeLayout>