安卓自定义组件视图带圆角

7

我想创建一个带有圆角的视图(并且可以使用不同的背景颜色重复使用),很难解释,所以这里是我的代码:

/app/src/com/packagename/whatever/CustomDrawableView.java


package com.packagename.whatever;

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

public class CustomDrawableView extends View {
    private PaintDrawable mDrawable;
    int radius;

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.RoundedRect);
        radius = a.getInteger(R.styleable.RoundedRect_radius, 0);
    }

    public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

        mDrawable = new PaintDrawable();
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.setCornerRadius(radius);
        mDrawable.draw(canvas);
    }
}

这是用于显示自定义组件的XML代码: /app/res/layout/test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ny="http://schemas.android.com/apk/res/com.packagename.whatever"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:padding="10dp">

    <com.packagename.whatever.CustomDrawableView
        android:id="@+id/custom"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#b80010"
        ny:radius="50"
    />

</LinearLayout>

我希望将红色的框框四个角变圆,它应该是50px的,但是您会看到,实际上并没有:

Red box without rounded corners

我的想法是,在XML中轻松更改背景颜色,并自动拥有一个带有圆角的漂亮视图,而不必创建多个可绘制对象。

感谢您的帮助!

3个回答

8
您需要将圆角半径和颜色设置到背景drawable中。
以下是一个可行的方式。获取您在android:background中设置的颜色,然后使用它来创建一个新的drawable,在构造函数中将其设置为背景。只要您将android:background设置为颜色值,这将起作用。
   public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

        // pull out the background color
        int color = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "background", 0xffffffff);

        // create a new background drawable, set the color and radius and set it in place
        mDrawable = new PaintDrawable();
        mDrawable.getPaint().setColor(color);
        mDrawable.setCornerRadius(radius);
        setBackgroundDrawable(mDrawable);
    }

如果你要重写onDraw方法,请确保首先调用super.onDraw(canvas)以绘制背景。

3

假设有这样一个简单的形状可绘制对象:

public ShapeDrawable Sd(int s){

float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null,null));

            mDrawable.getPaint().setColor(s);
return mDrawable;
}

您可以执行以下操作:

    LinearLayout l=(LinearLayout) findViewById(R.id.testLayout);
l.setBackgroundDrawable(Sd(0xff74AC23));

这里的“12”表示半径。 你可以将此应用于任何视图的背景可绘制。


如果您已经为按钮着色并需要将它们圆角化,可以使用以下代码:Resources res = this.getResources(); button2.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2))); - max4ever

2

谢谢回复,但我正在尝试动态设置背景颜色。你提供的前两个链接是我通常使用的方法,但只允许静态背景,这就是我的问题所在。第三个链接可能会解决问题...我会看一下的。 :) - iamkoa
@iamkoa 在调用 setBackgroundColor() 之后,无法生效吗? - Aleadam
是的,但这需要您添加一个android:background drawable,然后在Java中手动覆盖drawable的背景颜色,对吗?我正在寻找一种允许所有内容在一个XML调用中完成的解决方案。 - iamkoa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接