2016-10-22 3 views
9

描画可能な円形を作成しました。私はこれを私の線形レイアウトの背景として使用しています。それは正常に動作しています。しかし、問題は、色を変えて6つの円を作りたいということです。だから私は1つの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> 

です。

レイアウト: 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

この[post](http://stackoverflow.com/questions/16636412/change-shape-solid-color-at-runtime-inside-drawable-xml-used-as-background)をチェックしてください。 –

答えて

7

することができますあなたのコードでは、その後、すべてのボタンに同じ描画可能(指定したもの)を設定することによって:

例:

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の回答は良いですが、私は問題を解決する別の方法を追加したいと思います。 Javaでサークルビューを定義してから、このビューをXMLレイアウトで複数回使用し、必要に応じて色を変更することができます。 サークルビュー:

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

をそして、あなたはまた、あなたが彼らの背景を変更することができ、あなたのレイアウトで複数回、このビューを使用することができます:

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にこれらの行を追加します

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

次のスクリーンショットがあります。 enter image description here

+0

非常に良い答え。 +1 –

+0

@AbAppleticありがとうあなたの答えも完璧です;) –

関連する問題