java 在向后兼容的应用程序中使用 ?android:attr/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11383033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of ?android:attr/ in backwards compatible apps
提问by dsample
I'm trying to make my ICS (API level 15) app compatible with Gingerbread (API level 10), and I'm getting InflateException Error inflating class <Unknown>
for any layout XML that has an ?android:attr/
attribute. If I comment out these attributes I can compile and run the app, but then it, of course, looks aweful.
我正在尝试使我的 ICS(API 级别 15)应用程序与 Gingerbread(API 级别 10)兼容,并且我正在获取InflateException Error inflating class <Unknown>
任何具有?android:attr/
属性的布局 XML 。如果我注释掉这些属性,我就可以编译和运行该应用程序,但当然,它看起来很棒。
I'd rather not duplicate all of the items from android.R.attr
that I'm using, but at the moment I'm lost as to another way to do it.
我宁愿不复制android.R.attr
我正在使用的所有项目,但目前我迷失了另一种方法。
I'm using ActionBarSherlock to get the ActionBar working, and I don't seem to be using anything else that requires the support library (although I've included it during the process of trying to figure this out), it's just these theme-based resources that I'm stuck on.
我正在使用 ActionBarSherlock 来让 ActionBar 正常工作,而且我似乎没有使用任何其他需要支持库的东西(尽管我在尝试解决这个问题的过程中已经包含了它),这只是这些主题-基于我坚持的资源。
Some of the theme resources I'm using are:
我正在使用的一些主题资源是:
?android:attr/textColorSecondaryInverse
?android:attr/textAppearanceLarge
?android:attr/dividerVertical
?android:attr/selectableItemBackground
?android:attr/textAppearanceMedium
?android:attr/dividerVertical
?android:attr/dividerHorizontal
?android:attr/textColorSecondaryInverse
?android:attr/textAppearanceLarge
?android:attr/dividerVertical
?android:attr/selectableItemBackground
?android:attr/textAppearanceMedium
?android:attr/dividerVertical
?android:attr/dividerHorizontal
采纳答案by pawelzieba
As is in the documentationsome styles are in higher API. For example:
在文档中,一些样式在更高的 API 中。例如:
dividerVertical
since API 11dividerHorizontal
since API 11
dividerVertical
自 API 11dividerHorizontal
自 API 11
?
mark is used to reference style in current theme.
?
标记用于引用当前主题中的样式。
To deal with your problem you can:
要处理您的问题,您可以:
- use styles from API 11, but put them to
values-v11
folder and support styles for older versions invalues
using custom values or different attributes from older API. - copy necessary styles from ICS
- don't use these styles
- use custom styles
- 使用 API 11 中的样式,但将它们放入
values-v11
文件夹并支持旧版本的样式,以values
使用自定义值或来自旧 API 的不同属性。 - 从 ICS 复制必要的样式
- 不要使用这些样式
- 使用自定义样式
It depends what's your aim. First suggestion makes sense when native style of application is important for you.
If you want to have Holo style everywhere then there is no way than copy it and use as a one style for all platforms.
Take a look at this project: https://github.com/Prototik/HoloEverywhere
这取决于你的目标是什么。当原生应用程序风格对您很重要时,第一个建议是有意义的。
如果您想在任何地方都拥有 Holo 风格,那么除了复制它并将其用作所有平台的一种风格之外,别无他法。
看看这个项目:https: //github.com/Prototik/HoloEverywhere
回答by sandeep_jagtap
to use styles from API 11 Specifically android:attr/textAppearanceMedium ?android:attr/dividerVertical ?android:attr/dividerHorizontal
使用 API 11 中的样式特别是 android:attr/textAppearanceMedium ?android:attr/dividerVertical ?android:attr/dividerHorizontal
The easiest way is to use following code where ever your required
最简单的方法是在您需要的地方使用以下代码
<!-- 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"/>
回答by khigianguyen
Please check android support v7 - appcompat project. It has many themes and attributes for backwards compatibility (attr/dividerHorizontal also)
请检查 android support v7 - appcompat 项目。它具有许多向后兼容的主题和属性(也包括 attr/dividerHorizontal)
http://developer.android.com/tools/support-library/features.html#v7-appcompat
http://developer.android.com/tools/support-library/features.html#v7-appcompat
To use v7 support you must import it as an Android lib project and reference to it from you project. It also contains v4 support so you may want to remove v4 support in your libs folder :) Good luck!!
要使用 v7 支持,您必须将其作为 Android lib 项目导入并从您的项目中引用它。它还包含 v4 支持,因此您可能希望删除 libs 文件夹中的 v4 支持:) 祝您好运!!