【问题标题】:Can't inherit button color on style无法在样式上继承按钮颜色
【发布时间】:2015-11-01 12:27:52
【问题描述】:

我正在尝试在样式上定义按钮属性。 我的代码是

<style name="button">
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:textAllCaps">false</item>
    <item name="colorButtonNormal">#f37022</item>
    <item name="android:textColor">#ffffff</item>
</style>

使用 as

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Prepaid Package"
        style="@style/button"/>

但是按钮正常状态的颜色并没有改变。

此按钮样式仅适用于某些按钮。不适用于所有按钮。

现在该怎么办?

【问题讨论】:

    标签: android xml colors


    【解决方案1】:

    问题澄清后编辑

    将 style.xml 保存在 res/values/ 中

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="custom_button" parent="@android:style/Widget.Button">
            <item name="android:paddingTop">5dp</item>
            <item name="android:paddingBottom">5dp</item>
            <item name="android:textAllCaps">false</item>
            <item name="colorButtonNormal">#f37022</item>
            <item name="android:textColor">#ffffff</item>
        </style>
    

    像这样应用它:

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Prepaid Package"
        style="@style/custom_button"/>
    

    第一个答案

    你需要继承父样式。

    <style name="Button" parent="@android:style/Widget.Button">
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingBottom">5dp</item>
        <item name="android:textAllCaps">false</item>
        <item name="colorButtonNormal">#f37022</item>
        <item name="android:textColor">#ffffff</item>
    </style>
    

    这样,您所有未设置样式的 Button 都将具有您的自定义样式。即不需要输入样式值。

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Prepaid Package"/>
    

    【讨论】:

    • 这是否意味着colorButtonNormal将应用于所有按钮?
    • 但我不想为所有按钮应用这种样式。我想为一些按钮申请这种样式。
    • #f37022 的这种颜色 colorButtonNormal 出现在 on pressed 状态。
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2012-12-11
    • 2014-11-27
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多