2017-06-30 30 views
2

我正在设计一款游戏,需要让我的应用程序兼容API 16。我发现如何做AppCompatButton并设置样式,但是如何将颜色更改为更喜欢的颜色,如淡蓝色?AppCompatButton和Color

 <android.support.v7.widget.AppCompatButton 
     android:id="@+id/button7" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:elevation="1dp" 
     android:lines="2" 
     android:text="Button"/> 

感谢

回答

2

如果你将进入AppCompatButton类,你会看到有这样的Javadoc:

<ul> 
    <li>Supports {@link R.attr#textAllCaps} style attribute which works back to 
    {@link android.os.Build.VERSION_CODES#GINGERBREAD Gingerbread}.</li> 
    <li>Allows dynamic tint of it background via the background tint methods in 
    {@link android.support.v4.view.ViewCompat}.</li> 
    <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and 
    {@link R.attr#backgroundTintMode}.</li> 
</ul> 

,您可以设置backgroundTint属性在XML文件中游览按钮。就像这样:

<android.support.v7.widget.AppCompatButton 
    android:id="@+id/button7" 
    style="@style/Widget.AppCompat.Button.Colored" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:elevation="1dp" 
    android:lines="2" 
    android:text="Button" 
    app:backgroundTint="#555000"/>  <-- Here 
1

添加android:background属性为您的按钮声明与价值指的是颜色资源

android:background="@color/button_color" 

或指定颜色

android:background="#000000" 
+2

如果你设置背景颜色 - 你将失去所有的状态('state_pressed','state_enabled'等)和纹波在默认情况下按钮提供的效果(在21+以上)。 – Ekalips

0

定义风格是这样的:

<!-- put this in res/values/styles.xml --> 
<style name="StyledButton" parent="Widget.AppCompat.Button.Colored"> 
    <item name="android:textColor">@color/button_text</item> 
    <item name="colorButtonNormal">@color/button_background</item> 
</style> 

像往常一样应用到按钮:

<!-- apply style to button --> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:theme="@style/StyledButton"/> 

这应该与所有API级别兼容。

+0

感谢您的回复,但我需要在colors.xml中设置colorAccent –

0

想通了,我需要定义colorAccent:

 <color name="colorAccent">#448AFF</color>