【问题标题】:Android theme not being set未设置 Android 主题
【发布时间】:2011-02-09 16:34:09
【问题描述】:

我有一个拒绝应用于活动的主题 - 没有应用任何样式。如果我没有为<Button> 提供layout_width/layout_height 属性,它也会出现运行时错误,表明没有应用Button 类。

/res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:style/Theme.Black">
        <item name="android:windowNoTitle">true</item>
        <item name="android:buttonStyle">@style/Button</item>
        <item name="android:windowBackground">@color/page_background_light</item>
        <item name="android:textAppearance">@style/TextAppearance</item>
    </style>
</resources>

/res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@color/darkblue</item>
    </style>
    <style name="Button" parent="@android:style/Widget.Button">
        <item name="android:layout_width">fill_parent</item> 
        <item name="android:layout_height">wrap_content</item>
        <!--<item name="android:textColor">#3C2F4F</item>
        <item name="android:textSize">20dip</item>-->
    </style>
</resources>

以及相关的清单设置

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

我错过了什么明显的错误?

【问题讨论】:

  • 我现在正在处理完全相同的问题。您确定可以将主题放在单独的文件中吗?我尝试将主题设置为活动(而不是应用程序),并且我还尝试以编程方式应用它 onStart - 不走运...
  • 嗯。我认为我们有更大的问题。我试图从 parent="android:Theme.Light.NoTitleBar.Fullscreen" 继承,我得到了我的活动全屏。但是我的按钮和 ListView 样式仍然没有应用。所以,看起来主题“已应用”但不是里面的所有项目

标签: android themes android-manifest android-styles


【解决方案1】:

我也一直在努力解决这个问题,并认为我终于找到了可能的问题 - 也许是一样的。

我在res\values 文件夹中定义了自定义主题,但在values-v11values-v14 文件夹中没有。我认为这使得在某些设备中(特别是我正在测试的 2 台设备!),主题无法应用,因为它不存在。

我现在看到在我的自定义主题中设置的属性(在应用程序级别应用)生效。

【讨论】:

  • 谢谢!我快要疯了,想弄清楚我做错了什么。
  • 应该有一种方法可以让我们从一个 xml 文件中管理整个主题。
【解决方案2】:

我必须为每个&lt;activity&gt; 明确定义主题。

【讨论】:

    【解决方案3】:

    According to this answer,似乎无法通过样式提供layout_widthlayout_height。在编译时,它引发的异常是:

    E/AndroidRuntime(4117): java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.example/com.example.MainActivity}:
    java.lang.RuntimeException: Binary XML file line #79: You must supply a
    layout_width attribute.
    

    我不确定为什么会这样,但可能有一种解决方法。 As this question suggests,您也许可以提供一个参考作为宽度和高度参数。

    同样,我的经验是 Android 不正确支持通过样式提供小部件尺寸。

    【讨论】:

      【解决方案4】:

      只是出于好奇。尝试将您的主题重命名为 FooBar 之类的其他名称。

      在这里说明显而易见的事情,但请确保您的按钮使用正确的样式。

      【讨论】:

        【解决方案5】:

        编辑:我不确定这是否只是问题中的错字,但在您的主题中,您错误地引用了 Android 的 Theme.Black 样式。你的代码说:

        <style name="Theme" parent="android:style/Theme.Black">
        

        什么时候应该说:

        <style name="Theme" parent="@android:style/Theme.Black">
        

        我觉得 Lint 或编译器会在运行应用程序之前对你大吼大叫,但也许这会有所帮助。

        【讨论】:

        • 只要您的 xml 文件在 values 文件夹中,一切都应该没问题,并且文件名应该没有区别。
        • 查看官方文档后,您是正确的。但是,我确实发现了另一个可能的错误。现在更新。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        • 2012-07-31
        • 2015-03-07
        • 2018-02-14
        • 1970-01-01
        相关资源
        最近更新 更多