2016-10-22 31 views
9

Çizilebilir yuvarlak bir şekil oluşturdum. Bunu doğrusal düzenime arka plan olarak kullanıyorum. İyi çalışıyor. Ama sorun şu ki, farklı renklere sahip 6 daire oluşturmak istiyorum. Bu yüzden sadece bir tane çizilebilir şekil kullanabilir ve rengini farklı daireler için değiştirebilir miyim?Düzen dosyasındaki çizilebilir şeklin rengi nasıl değiştirilir

Bu benim çekilebilir dairesel şekil

Ben farklı renklerle çekilebilir dairesel bir şekil kullanarak bu düzen oluşturmak istiyorum
<?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> 

olduğunu.

düzen:

Örnek:

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); 
} 

bunu yapın Kodunuzdaki sonra, tüm düğmeleri aynı çekilebilir (Sağladığınız bir) ayarlayarak enter image description here

+0

Kontrol this post: http://stackoverflow.com/questions/40183852/defined-custom-shape-for-button-in-xml-now-i-want-to-change-the-color-dynamical –

+1

onay bu [post] (http://stackoverflow.com/questions/16636412/change-shape-solid-color-at-runtime-inside-drawable-xml-used-as-background). –

cevap

7

yapabilirsiniz düğmelerinizin her biri için, yourColorInt'u uyguladığınız düğmenin rengi ile değiştirmeyi unutmayın.

3

@AbAppletic yanıtı iyi olsa da, sorunu çözmek için başka bir yöntem eklemek istiyorum. Java'da daire görünümünü tanımlayabilir ve bu görünümü xml düzenlerinizde birden çok kez kullanabilir ve rengini istediğiniz gibi değiştirebilirsiniz. Circle Görünüm: kendi arka plan birden çok kez aynı zamanda değişir olabilir

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

Ve sonra düzeninde bu görünümü kullanabilirsiniz:

<?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 bu satırları ekleyin İşte

ekran görüntüsü: enter image description here

+0

Çok güzel cevap.); 1 –

+0

@AbAppletic teşekkürler cevap çok mükemmel –

İlgili konular