我想在UI中工作.我正在尝试为列表条目设置stateListDrawable.所有我想要做的是在按下项目时更改列表项布局的颜色,而在列表项目被按下的同时,我也想改变文本的颜色.
我收到以下错误堆栈:
- E/AndroidRuntime( 360): FATAL EXCEPTION: main
- E/AndroidRuntime( 360): android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
- E/AndroidRuntime( 360): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
- E/AndroidRuntime( 360): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
- E/AndroidRuntime( 360): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
- E/AndroidRuntime( 360): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
- E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
- E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
- E/AndroidRuntime( 360): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
充斥的XML如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/help_list_container"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="5dip"
- android:background="@drawable/default_list_selection">
- <TextView android:id="@+id/help_list_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16sp"
- android:textColor="@drawable/help_text_color">
- </TextView>
- </LinearLayout>
如果我从xml中删除android:textColor属性,我可以让程序工作.有没有办法可以使用stateListDrawable来控制xml中的listitem的texColor?
stateListDrawable适用于LinearLayout中的android:background,但它不适用于TextView的textColor属性.状态列表xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@color/white"
- android:state_pressed="true" />
- <item android:drawable="@color/black" />
- </selector>
任何回应都不胜感激.
解决方法
我错误地使用drawable来更改TextView的textColor.相反,我应该使用一个ColorStateList
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" android:color="@color/white" />
- <item android:color="@color/black"/>
- </selector>