简体   繁体   English

无法在Android中应用主题和样式

[英]Can't apply themes and styles in Android

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: 这是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: 这是AndroidManifest.xml中的代码:

<application android:icon="@drawable/icon" 
        android:label="@string/app_name"
        android:theme="@style/master">

and code in a ListView 和代码在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. 我知道eclipse不支持主题和样式的预览,但是它们也不能在模拟器上使用。

Help please! 请帮助! I can't believe I have been stuck with this tiny issue for a week... Thank you in advance! 我简直不敢相信我已经被这个小问题困扰了一周了……谢谢您!

There are a few things about Android styles and resources at work here. 这里有一些有关Android样式和资源的信息。

Android styles are an extremely general facility, and the result is that some configurations are possible but invalid and will be ignored. Android样式是一种非常通用的工具,其结果是某些配置是可能的,但无效并且将被忽略。 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 and textColor aren't valid on a ListView, so they are ignored. typefacetextColor在ListView上无效,因此将被忽略。 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: 完成您尝试做的事情的最佳方法可能是:

  • Create a style for TextViews. 为TextViews创建样式。 (This shouldn't have a parent that is a theme like your pasted code does.) (这不应像粘贴的代码那样具有作为主题的父对象。)
  • Apply that style to the TextView in your list item layout using the style= syntax. 使用style=语法将该style=应用于列表项布局中的TextView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM