当我尝试从TextView
中的属性而不是styles.xml
为colors.xml
分配颜色时,我遇到了一个奇怪的问题。
所以我有styles.xml
(摘录)定义“color_title”的颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="color_title" format="reference" />
<style name="DayTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="color_title">@color/buttonHoverBackground</item>
</style>
<style name="NightTheme" parent="Theme.AppCompat.NoActionBar">
<item name="color_title">@color/buttonBackground</item>
</style>
</resources>
如果我在普通布局中使用它,它可以正常工作。但是当我尝试在自定义数组适配器中使用的列表项中使用它来填充ListView时,我得到一个android.view.InflateException:
“二进制XML文件行#XX:错误扩充类 android.widget.TextView”。
这适用于适配器:
<TextView
android:id="@+id/ticker_article_title"
android:layout_width="match_parent"
android:textColor="@color/black"
android:layout_height="wrap_content"
android:textSize="@dimen/news_detail_info_box_title"/>
这不起作用
<TextView
android:id="@+id/ticker_article_title"
android:layout_width="match_parent"
android:textColor="?attr/color_title"
android:layout_height="wrap_content"
android:textSize="@dimen/news_detail_info_box_title"/>
所以我想知道这是否正常,或者为什么我不能在ArrayAdapter中使用样式属性,而它仍然适用于应用程序的其他部分。
编辑:这是完整的堆栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.mycompany.myapp, PID: 20045
android.view.InflateException: Binary XML file line #96: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at de.mycompany.myapp.screens.adapter.NewsOverviewTickerAdapter.getView(NewsOverviewTickerAdapter.java:90)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17429)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17429)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1488)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17429)
at com.ryanharter.viewpager.ViewPager.onMeasure(ViewPager.java:1511)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17429)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:17429)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2815)
at android.view.View.measure(View.java:17429)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:137
stacktrace(de.mycompany.myapp.screens.adapter.NewsOverviewTickerAdapter.getView(NewsOverviewTickerAdapter.java:90)
)中的第13行指向自定义ArrayAdapter中的此部分:
if (rootView == null) {
final LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rootView = inflater.inflate(
R.layout.fragment_newsoverview_ticker_listitem, null);
...
Edit2:这是一个示例,它显示View中的?attr/
和同一布局文件和片段中的ListView的用法。该按钮使用android:textColor="?attr/color_settings_sublabels"
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_meine_ressorts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="@dimen/news_detail_info_box_title"
android:textColor="?attr/color_settings_sublabels"
android:background="?attr/color_background"
android:text="@string/menu_favorites_edit"
android:textAllCaps="true"/>
<include layout="@layout/load_data_shimmer"
android:id="@+id/loadingPanel_initial"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/news_ticker_list"/>
</LinearLayout>
</RelativeLayout>