我最近在Android中尝试使用样式(并且公然失败)尝试在自定义视图背景资源中使用attrs。所以基本上,我想让用户设置一个自定义主题,如果他或她想。我有一个托管(支持)ViewPager的主片段。 ViewPager中的每个页面都使用自定义背景使用自定义视图进行充气。视图的背景指向样式参考,该参考基本上是由所选主题证明的颜色。
属性
<attr name="designBase400" format="reference" />
<attr name="designBase500" format="reference" />
<attr name="designBase700" format="reference" />
少数自定义样式之一
<style name="Theme.ColorScheme.Turquoise" parent="AppTheme">
<item name="designBase400">@color/design_base_400_turquoise</item>
<item name="designBase500">@color/design_base_500_turquoise</item>
<item name="designBase700">@color/design_base_700_turquoise</item>
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="windowActionBar">false</item>
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
</style>
对应的颜色
<color name="design_base_500_turquoise">#009688</color>
<color name="design_base_700_turquoise">#00796B</color>
<color name="design_base_400_turquoise">#26A69A</color>
后台资源
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="?attr/designBase400" />
</shape>
使用后台资源的视图(仅限重要部分)
<RelativeLayout
android:id="@+id/view_entry_hour_container"
android:layout_width="@dimen/entry_view_circle_dimen"
android:layout_height="@dimen/entry_view_circle_dimen"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="false"
android:background="@drawable/background_circle" >
<TextView
android:id="@+id/view_entry_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="@dimen/entry_view_hour_size"
android:textStyle="bold" />
</RelativeLayout>
现在我在ApplicationManifest.xml中设置了一个示例主题(比如@ style / Theme.ColorScheme.Turquoise),应用程序立即崩溃,引发了InflationException。 The stack trace can be found here.我对可能出现的问题没有最模糊的想法。
答案 0 :(得分:3)
实际上这是Android问题并且已在Lollipop版本中修复,请参阅Google问题跟踪链接,https://code.google.com/p/android/issues/detail?id=26251
所以你应该为每个主题创建单独的drawable。
答案 1 :(得分:2)
所以我有点使用earlier question上的回复解决了我的问题。显然,Android无法处理drawable中的属性引用(这非常不方便)。相反,应该创建对可绘制背景的引用,为每个主题创建一个drawable并将每个主题引用到相应的主题。基本上,我最终得到了以下设置:
后台资源
创建新参考
<attr name="designBase400" format="color" />
<attr name="designBase500" format="color" />
<attr name="designBase700" format="color" />
<!-- reference which points to the corresponding background resource -->
<attr name="circleBackground" format="reference" />
将资源引用添加到自定义主题
<style name="Theme.ColorScheme.Turquoise" parent="AppTheme">
<item name="designBase400">@color/design_base_400_turquoise</item>
<item name="designBase500">@color/design_base_500_turquoise</item>
<item name="designBase700">@color/design_base_700_turquoise</item>
<item name="circleBackground">@drawable/background_circle_turquoise</item>
</style>
<style name="Theme.ColorScheme.Blue" parent="AppTheme">
<item name="designBase400">@color/design_base_400_blue</item>
<item name="designBase500">@color/design_base_500_blue</item>
<item name="designBase700">@color/design_base_700_blue</item>
<item name="circleBackground">@drawable/background_circle_blue</item>
</style> <!-- and so on ... -->
我对这个解决方案不太满意,因为从逻辑上讲,我的第一次尝试应该完全没问题。我不能说为什么Android不接受drawables中的属性,我也许不会理解它。直到有人愿意启发并提供更好的解决方案,我想我必须坚持这一点。
答案 2 :(得分:0)
将format="reference"
更改为format="color"
。