【问题标题】:Android ignoring Style XML Background instructionAndroid忽略样式XML背景指令
【发布时间】:2015-12-12 21:09:30
【问题描述】:

我想创建一个橙色按钮样式,我将 style.xml 文件放置如下:

<style name="Button">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">16sp</item>
</style>
<style name="Button.OrangeButton">
    <item name="android:colorBackground">@drawable/orange_button</item>
    <item name="android:textColor">@color/white</item>

</style>

然后我将样式分配给一个按钮,如下所示:

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Broadcast"
    android:id="@+id/start_broadcast_button"
    android:layout_centerInParent="true"
    android:theme="@style/Button.OrangeButton"

    android:layout_marginTop="20dp"
    />

但是样式指令不会传递给按钮 - 它保留默认按钮格式。

我做错了什么?

【问题讨论】:

    标签: android xml android-styles


    【解决方案1】:

    改变

    android:theme="@style/Button.OrangeButton"
    

    style="@style/Button.OrangeButton"
    

    【讨论】:

    • 正确!谢谢!为什么样式不需要 android: prepend?
    • 我的猜测是省略了'android'前缀以表明该样式仅应用于此按钮。
    【解决方案2】:

    根据 Sparta 提供的答案,随着从 android:themestyle 的更改,您将需要更改

    <item name="android:colorBackground">@drawable/orange_button</item>
    

    <item name="android:background">@drawable/orange_button</item>
    

    【讨论】:

    • 是的。这是我为了“让它发挥作用”而进行的实验的残余。
    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2012-02-01
    • 1970-01-01
    • 2015-12-21
    • 2021-03-01
    相关资源
    最近更新 更多