Android 如何更改布局文件中可绘制形状的颜色

Android 如何更改布局文件中可绘制形状的颜色,android,android-layout,Android,Android Layout,我已经创建了一个可绘制的圆形。我用这个作为我的线性布局的背景。它工作得很好。但问题是,我想创建6个不同颜色的圆圈。所以我可以只使用一个可绘制的形状,并为不同的圆更改其颜色吗 这是我的可画圆形 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid

我已经创建了一个可绘制的圆形。我用这个作为我的线性布局的背景。它工作得很好。但问题是,我想创建6个不同颜色的圆圈。所以我可以只使用一个可绘制的形状,并为不同的圆更改其颜色吗

这是我的可画圆形

<?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 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);
}
Drawable-mDrawable=ContextCompat.getDrawable(context,R.Drawable.yourDrawable);
mDrawable.setColorFilter(新的PorterDuffColorFilter(yourColorInt,PorterDuff.Mode.MULTIPLY));
final int sdk=android.os.Build.VERSION.sdk\u int;
if(sdk

对每个按钮都执行此操作,但请记住将
yourColorInt
替换为要应用它的按钮所需的颜色。

虽然@AbAppletic answer很好,但我想添加另一种方法来解决此问题。您可以在java中定义一个圆形视图,然后在xml布局中多次使用该视图,并根据需要更改其颜色。 圆形视图:

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
中添加以下行:

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


您现在可以使用backgroundTint标签更改可绘制形状的颜色(API级别21)


保持相同的形状并应用不同的
app:backgroundTint

android:background="@drawable/shape"
app:backgroundTint="@color/blue"

注意
应用程序:
(自定义名称空间)有关更多信息,请查看此帖子:查看此。回答非常好+1@AbAppletic谢谢你的回答也很完美;)问题是轮廓/笔划也会改变颜色。
                android:backgroundTint="@color/yellow_color"
android:background="@drawable/shape"
app:backgroundTint="@color/blue"