2016-10-22 6 views
9

में खींचने योग्य आकार का रंग कैसे बदलें मैंने एक ड्रायबल सर्कुलर आकार बनाया है। मैं इसे अपने रैखिक लेआउट में पृष्ठभूमि के रूप में उपयोग कर रहा हूं। यह ठीक काम कर रहा है। लेकिन समस्या यह है कि, मैं विभिन्न रंगों के साथ 6 मंडल बनाना चाहता हूं। तो क्या मैं केवल एक खींचने योग्य आकार का उपयोग कर सकता हूं और विभिन्न रंगों के लिए अपना रंग बदल सकता हूं?लेआउट फ़ाइल

यह मेरा drawable गोल आकार

<?xml version="1.0" encoding="utf-8"?> 
<shape 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval" 
> 

<solid 
    android:color="@color/colorPrimary" 
    /> 
<size 
    android:width="30dp" 
    android:height="30dp"/> 
</shape> 

मैं इस लेआउट अलग अलग रंग के साथ drawable गोल आकार का उपयोग करते हुए निर्माण करना चाहते है।

लेआउट: enter image description here

+0

चेक इस पोस्ट: http://stackoverflow.com/questions/40183852/defined-custom-shape-for-button-in-xml-now-i-want-to-change-the-color-dynamical –

+1

यह [पोस्ट] देखें (http://stackoverflow.com/questions/16636412/change-shape-solid-color-at-runtime-inside-drawable-xml-used-as-background)। –

उत्तर

7

आप कर सकते हैं अपने कोड में, ही drawable सभी बटन (एक आपके द्वारा दी गई) की स्थापना तब तक:

उदाहरण:

Drawable mDrawable = ContextCompat.getDrawable(context, R.drawable.yourDrawable); 
mDrawable.setColorFilter(new PorterDuffColorFilter(yourColorInt,PorterDuff.Mode.MULTIPLY)); 

final int sdk = android.os.Build.VERSION.SDK_INT; 
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
    yourButton.setBackgroundDrawable(mDrawable); 
} else { 
    yourButton.setBackground(mDrawable); 
} 

यह करें अपने प्रत्येक बटन के लिए, लेकिन जिस रंग को आप इसे लागू कर रहे हैं उसके लिए इच्छित रंग के साथ yourColorInt को प्रतिस्थापित करना याद रखें।

3

हालांकि @AbAppletic जवाब अच्छा है, मैं समस्या को हल करने का एक और तरीका जोड़ना चाहते हैं। आप जावा में एक सर्कल व्यू को परिभाषित कर सकते हैं और फिर अपने एक्सएमएल लेआउट में इस बार कई बार उपयोग कर सकते हैं और अपनी इच्छानुसार अपना रंग बदल सकते हैं। द सर्कल दृश्य: कई बार उनकी पृष्ठभूमि

<declare-styleable name="Circle"> 
     <attr name="circleRadius" format="integer"/> 
     <attr name="circleColor" format="color" /> 
    </declare-styleable> 

और फिर आप अपने लेआउट में इस दृश्य का उपयोग कर सकते हैं, यह भी आप बदल सकते हैं:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical"> 

<TableRow android:gravity="center"> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color1" /> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv2" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color2" /> 

</TableRow> 

<TableRow android:gravity="center"> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv3" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color3" /> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv4" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color4" /> 

</TableRow> 

<TableRow android:gravity="center"> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv5" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color5" /> 

    <com.afranet.broadbandportal.view.Circle xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv6" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:layout_margin="5dp" 
     android:background="@color/colorPrimary" 
     app:circleColor="@color/color6" /> 

</TableRow> 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 


public class Circle extends View { 

Paint p; 
int color ; 
public Circle(Context context) { 
    this(context, null); 
} 

public Circle(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public Circle(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    // real work here 
    TypedArray a = context.getTheme().obtainStyledAttributes(
      attrs, 
      R.styleable.Circle, 
      0, 0 
    ); 

    try { 

     color = a.getColor(R.styleable.Circle_circleColor, 0xff000000); 
    } finally { 
     // release the TypedArray so that it can be reused. 
     a.recycle(); 
    } 
    init(); 
} 

public void init() 
{ 
    p = new Paint(); 
    p.setColor(color); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 
    if(canvas!=null) 
    { 
     canvas.drawCircle(getHeight()/2, getWidth()/2,getWidth()/2,p); 
    } 
} 

} 

इन पंक्तियों attrs.xml में जोड़ें

यहाँ स्क्रीनशॉट है: enter image description here

+0

बहुत अच्छा जवाब। +1 –

+0

@AbAppletic धन्यवाद आपका उत्तर भी सही है;) –

संबंधित मुद्दे