AppCompatButton和Color

时间:2017-06-30 15:13:02

标签: android android-layout android-styles

我正在设计一款游戏,需要让我的应用程序与API 16兼容。我找到了如何做AppCompatButton并设置样式但是如何将颜色更改为更令人愉悦的颜色,如浅蓝色?

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

感谢

4 个答案:

答案 0 :(得分: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文件中的tour按钮。像这样:

 <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 :(得分:1)

android:background属性添加到按钮声明中,其值为引用颜色资源

android:background="@color/button_color"

或指定颜色

android:background="#000000"

答案 2 :(得分: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级别兼容。

答案 3 :(得分:0)

想出来,我需要定义colorAccent:

     <color name="colorAccent">#448AFF</color>
相关问题