6

我正在关注有关创建自定义按钮的教程。如果我直接将样式应用到按钮上,它可以工作,但是如果我将样式应用到使用它的主题上,android:buttonStyle它就行不通了。

一个示例 - 这是应用了按钮样式的样式 xml:values/styles.xml

<resources>
    <style name="MyTheme" parent="android:Theme.Light">
       <!-- the button gets styled but is no longer clickable 
            if i do it like this -->
       <item name="android:buttonStyle">@style/ButtonText</item>
    </style>

    <style name="ButtonText">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:background">@drawable/ruddy_orange_button</item>
    </style>
</resources>

可绘制/ruddy_orange_button.xml:

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#70c656" />
            <stroke android:width="1dp" android:color="#53933f" />
            <corners android:radius="3dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
   <item>
        <shape>
            <gradient android:angle="270" android:endColor="#fd4d4d" android:startColor="#f24b4b" />
            <stroke android:width="1dp" android:color="#f04a4a" />
            <corners android:radius="4dp" />
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape> 
    </item>
</selector>

但是,如果我将样式直接应用于按钮,它会起作用:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/ButtonText"
    android:text="hello there" />

关于我在这里做错了什么的任何指示?

我在 Android 2.3.3 上测试了这个

来源在github 上

4

2 回答 2

9

尝试这个:

<style name="ButtonText" parent="@android:style/Widget.Button">
于 2011-12-08T16:19:56.370 回答
6

我也有这个问题。希望小费能帮助你

appcompat v21+ 你应该使用buttonStyle而不是android:buttonStyle在你的主题中

于 2015-07-30T11:09:54.967 回答