自定义view出现Binary XML file line #:error inflating class错误

开始学习自定义view的写法,首先新建一个类CircleView继承自View类,完成三类带参构造器,重写ondraw方法,在布局文件中加入自定义view控件,Run之后出现Binary XML file line #:error inflating class XXX错误,原因是没有加入xmlns:custom="http://schemas.android.com/apk/res/com.ryg.chapter_2.viewtext3

这属于是一种约束,规定xml中有哪些元素和他们的关系,需要在布局中添加进去,下面是代码

public class CircleView extends View {
    private int mColor = Color.RED;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CircleView(Context context){
        super(context);
        init();
    }

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

    public CircleView(Context context, AttributeSet attrs,int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint.setColor(mColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width,height) / 2;
        canvas.drawCircle(width / 2,height / 2, radius, mPaint);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.ryg.chapter_2.viewtext3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <com.ryg.chapter_2.viewtext3.CircleView
        android:id="@+id/circleView1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000" />

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值