2017-06-30 2 views
2

Ich entwerfe ein Spiel und muss meine App kompatibel zu API 16 machen. Ich habe herausgefunden, wie man den AppCompatButton ausführt und den Stil festlegt, aber wie ändere ich die Farbe in eine angenehmere Farbe wie ein Hellblau ?AppCompatButton und 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"/> 

dank

Antwort

2

Wenn Sie in AppCompatButton Klasse gehen werden Sie diese javadoc sehen es werde:

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

So können Sie einstellen, backgroundTint Attribut Schaltfläche in XML-Datei auf Tour. Wie folgt aus:

<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

Add android:background Attribut auf Ihre Schaltfläche Erklärung mit Wert bezogen auf die Farbressource

android:background="@color/button_color" 

Oder Angabe Farbe

android:background="#000000" 
+2

Wenn Sie Hintergrund Farbe eingestellt werden - Sie alle Zustand verlieren werden ('state_pressed',' state_enabled' und so weiter) und Welligkeit Effekte (ab 21+), die standardmäßig in der Schaltfläche enthalten sind. – Ekalips

0

definieren einen Stil wie folgt aus:

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

Nehmen, um die Taste wie gewohnt:

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

Dies sollte mit allen API-Ebenen kompatibel sein.

+0

Vielen Dank für Ihre Antwort, aber ich musste den colorAccent in der colors.xml einstellen –

0

es heraus, ich brauchte die colorAccent zu definieren:

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