14

我正在尝试使我的 ICS(API 级别 15)应用程序与 Gingerbread(API 级别 10)兼容,并且我正在获取InflateException Error inflating class <Unknown>任何具有?android:attr/属性的布局 XML。如果我注释掉这些属性,我可以编译并运行应用程序,但它当然看起来很棒。

我宁愿不复制android.R.attr我正在使用的所有项目,但目前我迷失了另一种方法。

我正在使用 ActionBarSherlock 来让 ActionBar 工作,而且我似乎没有使用任何其他需要支持库的东西(尽管我在尝试解决这个问题的过程中已经包含了它),只是这些主题 -基于我坚持的资源。

我正在使用的一些主题资源是:

?android:attr/textColorSecondaryInverse ?android:attr/textAppearanceLarge ?android:attr/dividerVertical ?android:attr/selectableItemBackground ?android:attr/textAppearanceMedium ?android:attr/dividerVertical ?android:attr/dividerHorizontal

4

3 回答 3

12

使用 API 11 中的样式特别是 android:attr/textAppearanceMedium ?android:attr/dividerVertical ?android:attr/dividerHorizo​​ntal

最简单的方法是在您需要的地方使用以下代码

<!-- For Horizontal Line-->
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="#aaa"
android:layout_alignParentTop="true"/>

<!-- For Vertical Line-->

<View
android:id="@+id/VerticalLine"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="#aaa"/>
于 2013-02-01T18:09:49.133 回答
11

文档中一样,某些样式位于更高的 API 中。例如:

  • dividerVertical自 API 11 起
  • dividerHorizontal自 API 11 起

?标记用于引用当前主题中的样式。

要解决您的问题,您可以:

  • 使用 API 11 中的样式,但将它们放入values-v11文件夹并支持旧版本的样式,以values使用自定义值或旧 API 的不同属性。
  • 从 ICS 复制必要的样式
  • 不要使用这些样式
  • 使用自定义样式

这取决于你的目标是什么。当应用程序的原生风格对您很重要时,第一个建议是有意义的。
如果你想在任何地方都有 Holo 风格,那么没有办法复制它并将其用作所有平台的一种风格。
看看这个项目:https ://github.com/Prototik/HoloEverywhere

于 2012-07-08T12:34:26.900 回答
3

请检查 android support v7 - appcompat 项目。它有许多向后兼容的主题和属性(attr/dividerHorizo​​ntal 也是)

http://developer.android.com/tools/support-library/features.html#v7-appcompat

要使用 v7 支持,您必须将其作为 Android lib 项目导入并从您的项目中引用它。它还包含 v4 支持,因此您可能希望在 libs 文件夹中删除 v4 支持 :) 祝你好运!

于 2013-10-20T15:30:26.853 回答