20

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

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

4

5 回答 5

23

我也一直在为此苦苦挣扎,并认为我终于找到了可能存在的问题-也许是一样的。

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

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

于 2012-11-18T19:51:31.850 回答
4

我必须为每个<activity>.

于 2012-09-14T07:37:06.933 回答
3

根据这个答案,似乎无法提供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.

我不确定为什么会这样,但可能有一种解决方法。正如这个问题所暗示的,您可以提供参考作为宽度和高度参数。

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

于 2012-09-25T09:26:54.223 回答
0

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

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

当它应该说:

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

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

于 2012-09-21T17:10:54.333 回答
0

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

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

于 2011-09-22T16:46:44.717 回答