2015-11-01 34 views
2

我想要定义样式上的按钮属性。 我的代码是无法继承样式上的按钮颜色

<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> 

而且使用作为

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

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

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

现在该做什么?问题澄清后

回答

1

编辑

保存您在res /值style.xml/

<?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 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Prepaid Package"/> 
+0

这是否意味着'colorButtonNormal'将应用于所有按钮? – Sudarshan

+0

@Sudarshan你创建的新风格将适用于所有的按钮,除非你给他们一个新的风格。如果这不起作用,那么这意味着你已经错误地设置了你的样式资源。 –

+0

但我不想将这种风格应用于所有按钮。 我想为某些按钮申请此样式。 – Sudarshan