編輯我做了建議的更改,但仍然收到相同的錯誤,如下所示。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)...
看到我的答案有錯誤,當你聲明你的顏色。 – Fllo
在你的XML中,你已經設置了layout_height =「fill_pare」,如果你把fill_parent(或者更好的match_parent)仍然存在錯誤?如果錯誤仍然存在,您可以發佈崩潰活動的onCreate方法的代碼? – jaumebd
@jaumebd我認爲你只是找到解決方案! ^^ – Fllo