2017-05-16 4 views
3

Ich habe diese Xml, wo ich bereits Strichfarbe festlegen, aber ich möchte manchmal stoke Farbe programmgesteuert ändern. Der Dateiname dummy.xmlJava Android Farbe in Xml-Datei ändern

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff" /> 

    <stroke 
     android:width="1dp" 
     android:color="#ff000000" /> 

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

    <corners 
     android:bottomLeftRadius="7dp" 
     android:bottomRightRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 
</shape> 

ist möchte ich Farbe (Strichfarbe) programmatisch ändern, wie kann ich dies tun?

Ich benutze diese xml hier:

<LinearLayout 
       android:layout_marginTop="2dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/a" 
       android:layout_gravity="center" 
       android:background="@drawable/dummy" 
       android:layout_marginBottom="2sp" 
       android:orientation="horizontal" 
       android:padding="5dp"> 

       <RelativeLayout 
        android:id="@+id/serverStatusWrapper" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="2dp" 
        android:background="@drawable/shadow_green" 
        android:padding="2dp"> 

        <ImageView 
         android:id="@+id/serverStatus" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:antialias="true" 
         android:src="@drawable/ic_settings_input_antenna_white_24dp" /> 

       </RelativeLayout> 

       <RelativeLayout 
        android:id="@+id/locationStatusWrapper" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/shadow_red" 
        android:padding="2dp"> 

        <ImageView 
         android:id="@+id/locationStatus" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:antialias="true" 
         android:src="@drawable/ic_warning_white_24dp" /> 
       </RelativeLayout> 

      </LinearLayout> 
+0

Sie benötigen ziehbar erstellen dynamisch habe ich versuchen, dies aber nicht für mich arbeiten –

Antwort

3
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a); 

Sie können die Hubwechsel als

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground(); 
drawable.setStroke(3, Color.RED); 

Und kann feste Farbe wie

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground(); 
drawable.setColor(Color.RED); 
012 ändern

hoffe, das hilft

+0

Ok, aber diese xml ich in Linearlayout verwenden –

+0

dann Imageview ändern –

+0

zu Linearlayout ich das versuchen, aber ich habe Nullpointer: GradientDrawable zeichnungsfähig = (GradientDrawable) linearLayout.getBackground(); –

Verwandte Themen