2015-10-13 23 views
0

我试图在我的应用中为按钮创建样式。为了做到这一点,我创建了一个带有角标签的可绘制文件。我还为不同的按钮状态(正常和禁用)创建了不同的样式。当我在不设置背景属性的情况下应用样式时,按钮具有正确的颜色。但是,当我用我的可绘制文件设置背景属性时,style.xml中定义的文本颜色起作用,但不起作用。下面是代码:

值/ style.xml:当定义形状时,Android不应用背景色

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:background">@color/background</item> 
    <item name="android:textColor">@color/text_color</item> 
</style> 

<style name="ButtonStyle"> 
    <item name="android:background">@color/button_color</item> 
</style> 

<style name="ButtonDisableStyle"> 
    <item name="android:background">@color/button_color_disabled</item> 
    <item name="android:textColor">@color/buttonText_color_disabled</item> 
</style> 

绘制/ button_shape.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners 
    android:radius="5dp" /> 

<padding 
    android:left="0dp" 
    android:top="0dp" 
    android:right="0dp" 
    android:bottom="0dp" /> 
</shape> 

main_activity:

<Button 
    android:id="@+id/newButton" 
    android:text="@string/generate_first" 
    style="@style/ButtonStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="30" 
    android:background="@drawable/button_shape" 
    /> 

我看到差不多的几个后问题,我知道可以在drawable/button_shape.xml中定义按钮的背景颜色,但是我想保留这个样式并且使其工作(我想要的形状和风格分开)。
你有什么想法吗?

+0

出现这种情况的原因是因为你是压倒一切通过为您的视图设置一个明确的风格背景。我不确定在你的情况下使用xml的方法。你可以创建一个自定义的按钮视图并从那里控制事物,但它不是对你的问题的直接回答。 – zgc7009

+0

做我想在这里做的事情就是让我通过改变风格来改变所有的应用程序设计。由于我的设计是暂时的,我不想创建自定义视图,但我可能会在最终版本中完成。但是,谢谢你的回答。也许我问的是不可能的。无论如何,我会继续搜索。 – Mtoypc

回答

0

最后,经过更多的研究,我找到了一个解决方案。我为对应于3种不同状态的按钮创建了3个xml形状文件。然后我创建了第4个xml形状文件,其中包含3个其他文件。 最后,我将对第四个形状xml文件的引用放入按钮样式中(在values/style.xml中)。下面是代码:

抽拉/ button_disabled.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<solid android:color="@color/buttonText_color_disabled"/> 
<padding android:left="7dp" 
    android:top="7dp" 
    android:right="7dp" 
    android:bottom="7dp" /> 
<corners android:radius= "5dp" /> 
</shape> 


抽拉/ button_pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="@color/button_color_pressed"/> 
    <padding android:left="7dp" 
     android:top="7dp" 
     android:right="7dp" 
     android:bottom="7dp" /> 
    <corners android:radius= "5dp" /> 
</shape> 


抽拉/ button_enabled.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="@color/button_color"/> 
    <padding android:left="7dp" 
     android:top="7dp" 
     android:right="7dp" 
     android:bottom="7dp" /> 
    <corners android:radius= "5dp" /> 
</shape> 


绘制/ button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_enabled="false" 
     android:drawable="@drawable/button_disabled" /> 
    <item 
     android:state_pressed="true" 
     android:state_enabled="true" 
     android:drawable="@drawable/button_pressed" /> 
    <item 
     android:state_enabled="true" 
     android:drawable="@drawable/button_enabled" /> 
</selector> 


值/ style.xml:

<style name="Custom_button" parent="@android:style/Widget.Button"> 
     <item name="android:gravity">center_vertical|center_horizontal</item> 
     <item name="android:textColor">@color/text_color</item> 
     <item name="android:textSize">16dip</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:background">@drawable/button</item> 
     <item name="android:clickable">true</item> 
    </style> 


main_activity:

<Button 
     android:id="@+id/newButton" 
     android:text="@string/generate_first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_below="@id/app_info" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="35dp" 
     android:layout_marginBottom="50dp" 
     style="@style/Custom_button" />