无法在 Android 中应用主题和样式
我已经搜索了几个小时的解决方案:如何将简单的主题或样式应用于应用程序、活动或只是视图?这看起来非常简单,但我的风格总是被忽视。
这是 style.xml 中的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="master" parent ="@android:style/Theme.NoTitleBar">
<item name="android:typeface">serif</item>
<item name="android:textColor">#8b8378</item>
</style>
</resources>
这是 AndroidManifest.xml 中的代码:
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/master">
以及 ListView 中的代码
<ListView android:layout_height="wrap_content"
style="@style/master"
android:id="@+id/list"
android:layout_width="fill_parent">
</ListView>
什么也没有发生。字体样式、颜色均保持默认。只有像这样显式声明属性
<TextView android:gravity="center" android:typeface="serif" android:textColor="#8b7d6b" android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/text_intro" android:layout_height="wrap_content" android:text="@string/welcome_text" android:layout_width="wrap_content" android:padding="20sp" android:layout_weight="0"></TextView>
才有效。我知道 eclipse 不支持主题和样式的预览,但它们也不适用于模拟器。
请帮忙!我不敢相信我已经被这个小问题困扰了一个星期......提前谢谢您!
I've been searching the solution for hours: how to apply a simple theme or style to an application, an activity or just a view? It seems super easy but my styles always get ignored.
Here is the code in style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="master" parent ="@android:style/Theme.NoTitleBar">
<item name="android:typeface">serif</item>
<item name="android:textColor">#8b8378</item>
</style>
</resources>
and here is the code in AndroidManifest.xml:
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/master">
and code in a ListView
<ListView android:layout_height="wrap_content"
style="@style/master"
android:id="@+id/list"
android:layout_width="fill_parent">
</ListView>
NOTHING ever happens. Font style, color all remain the default. Only by declare the attributes explicitly like
<TextView android:gravity="center" android:typeface="serif" android:textColor="#8b7d6b" android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/text_intro" android:layout_height="wrap_content" android:text="@string/welcome_text" android:layout_width="wrap_content" android:padding="20sp" android:layout_weight="0"></TextView>
will work. I know eclipse doesn't support preview of theme and style, but they don't work on emulator as well.
Help please! I can't believe I have been stuck with this tiny issue for a week... Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些关于 Android 风格和资源的内容。
Android 样式是一种极其通用的设施,其结果是某些配置是可能的但无效并且将被忽略。你已经遇到过几个了。 :)
当应用于视图时,样式相当于在该视图上设置相同的属性。 样式不会级联到子视图。
typeface
和textColor
在 ListView 上无效,因此会被忽略。他们也不会以这种方式处理主题,因为主题为不同类型的视图提供了默认样式。 (为什么会默默地忽略无效属性而不是生成错误?因为随着在以后的平台修订中添加新属性,旧设备应该忽略它们不知道如何解析兼容性的额外属性。)完成您的任务的最佳方法正在尝试做的可能是:
style=
语法将该样式应用到列表项布局中的 TextView。There are a few things about Android styles and resources at work here.
Android styles are an extremely general facility, and the result is that some configurations are possible but invalid and will be ignored. You've run across a few. :)
When applied to a view, a style will be equivalent to setting those same attributes on that view. Styles do not cascade to child views.
typeface
andtextColor
aren't valid on a ListView, so they are ignored. They also aren't going to work on a theme that way, since themes provide default styles for different kinds of views. (Why are invalid attributes silently ignored instead of generating an error? Because as new attributes are added in later platform revisions, older devices should ignore extra attributes that they don't know how to parse for compatibility.)The best way to accomplish what you're trying to do is likely to be:
style=
syntax.