android - 通过布局更改 xml 可绘制文件中形状的颜色

标签 android android-layout drawable android-custom-view

所以我创建了这个名为 rounded_rect 的可绘制 xml 文件。我最初将其颜色设置为红色。但是我希望能够在我的布局文件中更改这种颜色。

rounded_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#ffff1900"/>
<corners
    android:bottomRightRadius="15dp"
    android:bottomLeftRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp"/>

<stroke android:width="4dp"
    android:color="#000000"/>
</shape>

我的布局文件包含一个名为 ItemView 的自定义 View 。我的自定义属性之一是背景颜色。

<custonview.android.example.com.customview.ItemView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    custom:imagePath="@drawable/yes"
    custom:text="Yes"

    custom:backgroundColor="#000000"

    android:layout_row="0"
    android:layout_column="0"
    android:background="@drawable/rounded_rect_green"
    android:layout_margin="30dp" />

目标是能够将可绘制文件中的颜色更改为我在布局文件中指定的颜色。这是我的 ItemView.java 文件

public class ItemView extends View {

    //attributes of item view
    private Drawable drawable;
    private String text;
    private int backgroundColor;

    private int width, height;
    private Paint paint;

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.ItemView, 0, 0);
        try {
            text = a.getString(R.styleable.ItemView_text);
            backgroundColor = a.getInteger(R.styleable.ItemView_backgroundColor, 0);
            drawable = a.getDrawable(R.styleable.ItemView_imagePath);
        } finally {

        }
        paint = new Paint();
        width = drawable.getMinimumWidth() / 2;
        height = drawable.getMinimumHeight() / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //positions the image in the middle horizontally and a little above the text below
        drawable.setBounds(width / 4, height / 4 - 50, width - (width / 4),
                height - (height / 4) - 50);
        drawable.draw(canvas);

        paint.setTextSize(100);
        paint.setColor(Color.BLACK);
        canvas.drawText(text, width / 2 -
                paint.measureText(text, 0, text.length()) / 2, height - 50, paint);
    }
}
  • 也许我考虑过度了。在您的答案中自由发挥创意。提前致谢

最佳答案

试试下面的代码-

GradientDrawable bgShape = (GradientDrawable)yourview.getBackground();
        bgShape.setColor(Color.MAGENTA);

关于android - 通过布局更改 xml 可绘制文件中形状的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362211/

相关文章:

Android 应用程序图标前景默认为 android Logo

Android: ScrollView 中的相对布局

android - 添加 CornerPathEffect 时 FillType.EVEN_ODD 的不同行为?

java - 从 SD 卡加载绘图

android - ProGuard,解析异常,NoSuchMethodError : android.net.SSLCertificateSocketFactory.getHttpSocketFactory

android - 导航查看菜单项,例如 Google Play 商店

Android Studio 无法启动应用程序

java - 如何在 Android 中为动态选择的图像创建 savedInstanceState?

android 获取联系人详细信息,例如号码和缩略图

c# - 如何从字符串中读取特定行?统一 C#