我想从xml中扩展一个视图,就像这样:
View textView = LayoutInflater.from(context).inflate(R.layout.item_classify_left, null);
但我在代码上遇到错误:
android.view.InflateException: Binary XML file line #2: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.inflate(LayoutInflater.java:472)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
有布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dip"
android:gravity="center"
android:textColor="@drawable/item_classify_left_textcolor_selector"
android:background="@drawable/item_classify_left_background_selector" />
我发现当我删除android:textColor和android:background时,它工作正常。但我真的需要选择器。有选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/main_background_color" />
<item android:color="@color/item_classify_text_background_normal" />
</selector>
我该如何解决这个问题?
答案 0 :(得分:3)
将 item_classify_left_textcolor_selector.xml 放到文件夹res/color
,然后使用@color/item_classify_left_textcolor_selector
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dip"
android:gravity="center"
android:textColor="@color/item_classify_left_textcolor_selector"
android:background="@drawable/item_classify_left_background_selector" />