2016-06-28 7 views
2

Ich habe einen Selektor, wo ich einen Kreis als Hintergrund für die State_selected = true festlegen, aber ich möchte die Farbe ändern, wenn ich auf das Objekt klicken. Wie kann ich es tun? DieseAndroid - Ändern der Farbe von Drawables

ist, wie mein Drawables eingerichtet sind:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="#D0021B"/> 
<stroke android:width="2dp" android:color="#910012" /> 
<size 
    android:width="50dp" 
    android:height="50dp" /> 
<corners android:radius="50dp" /> 
</shape> 

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@android:color/black" android:state_selected="false" /> 
<item android:drawable="@drawable/nav_circle" android:state_selected="true" /> 
</selector> 
+0

Verwendung state_pressed ..... –

+0

Wie kann ich die Farbe gesetzt obwohl dynamisch zu sein? – Mikerizzo

Antwort

0

Sie können es programmatisch tun:

public static void colorDrawable(View view, int color) { 
      Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground()); 
      if (wrappedDrawable != null) { 
       DrawableCompat.setTint(wrappedDrawable.mutate(), color); 
       setBackgroundDrawable(view, wrappedDrawable); 
      } 
     } 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static void setBackgroundDrawable(View view, Drawable drawable) { 

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
     view.setBackgroundDrawable(drawable); 
    } else { 
     view.setBackground(drawable); 
    } 
} 
0

, wenn Sie die Farbe im Moment zu ändern, gedrückt ist, können Sie state_pressed verwenden.

<item android:state_pressed="true" 
     android:drawable="@color/pressedColor"/> <!-- pressed state --> 
<item android:state_focused="true" 
     android:drawable="@color/pressedColor"/> <!-- focused state --> 
<item android:drawable="@color/colorPrimary"/> 

Sie können auch eine andere ziehbar unter dem Android verwenden: ziehbar Attribut

<item android:state_pressed="true" 
     android:drawable="@drawable/button_solid"/> 

jedoch, wenn Sie den Hintergrund ziehbar ändern wollen, wenn sie gedrückt wird, können Sie auf die Schaltfläche Hintergrund ändern, um eine andere xml ziehbar

button.setBackground(getDrawable(R.drawable.selectorDrawable2)); 
+0

Ich muss in der Lage sein, eine dynamische Farbe aus dem Server – Mikerizzo

0

Vielleicht wird dies Ihnen helfen:

Image xml-Code:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/nav_circle" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/image" /> 
+0

im Erzielen einer android.graphics.drawable.StateListDrawable kann nicht auf android.graphics.drawable.GradientDrawable error – Mikerizzo

+0

können Sie versuchen, dass ohne Gießen: 'i. getBackground(). setColorFilter (Color.BLUE, PorterDuff.Mode.SRC_ATOP); 'Kannst du auch meinen XML-Code sehen? –