android:attr 样式如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10869184/
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
How does an android:attr style work?
提问by Hounshell
I'm trying to build an Android UI via layouts. I start with the following:
我正在尝试通过布局构建 Android UI。我从以下几点开始:
<TextView
android:id="@+id/..."
android:layout_marginTop="8dip"
android:text="..."
style="?android:attr/listSeparatorTextViewStyle"/>
And that looks good (all caps, smaller font, dividing bar underneath it). Now I want to extend the style, so I change it to the following:
这看起来不错(全部大写,字体较小,下方的分隔条)。现在我想扩展样式,所以我将其更改为以下内容:
<TextView
android:id="@+id/..."
android:layout_marginTop="8dip"
android:text="..."
style="@style/section_title"/>
With a style of:
风格如下:
<style name="section_title" parent="@android:attr/listSeparatorTextViewStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
And that doesn't work (font is correct, but the divider line is gone).
这不起作用(字体是正确的,但分隔线不见了)。
How come... that?
怎么会……那个?
回答by Luksprog
When you're using:
当您使用:
style="?android:attr/listSeparatorTextViewStyle"
you're using the style pointed by this attribute(listSeparatorTextViewStyle
). If you look in the platform themes.xml
you'll see that the style that is actually used for this attribute is Widget.TextView.ListSeparator.White
. So this is the style
you should extend in your custom style.
您正在使用此属性( listSeparatorTextViewStyle
)指向的样式。如果您查看平台,themes.xml
您会看到实际用于此属性的样式是Widget.TextView.ListSeparator.White
. 所以这是style
你应该在你的自定义样式中扩展的。
Unfortunately that style is private and you can't extend it, or you shouldn't extend it(for reference, see this bug reportfrom google). Your best option would be to copy that entire style, Widget.TextView.ListSeparator.White
(Widget.TextView.ListSeparator
isn't public as well so would have to also copy that), in your custom style and use that instead of extending the style from the android platform(see this responsefrom the link above).
不幸的是,这种风格是私有的,你不能扩展它,或者你不应该扩展它(作为参考,请参阅谷歌的这个错误报告)。您最好的选择是在您的自定义样式中复制整个样式Widget.TextView.ListSeparator.White
(Widget.TextView.ListSeparator
也不是公开的,因此也必须复制)并使用它而不是从 android 平台扩展样式(请参阅链接中的此回复以上)。