下面是我的自定义视图代码。我基本上是在创建一个圆圈并将一些文字放入其中。即使我已经重载了两个构造函数,我也得到了布局没有膨胀的错误。
package com.example.customview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
@SuppressWarnings("unused")
public class PieChart extends View {
String name = new String();
int color = 0;
public PieChart(Context context)
{
this(context,null);
}
@SuppressLint("InlinedApi")
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PieChart, 0, 0);
name = a.getString(R.styleable.PieChart_Name);
color = a.getInt(R.styleable.PieChart_color, 0);
a.recycle();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
Paint circlePaint = new Paint();
int viewWidthHalf = this.getMeasuredWidth()/2;
int viewHeightHalf = this.getMeasuredHeight()/2;
int radius = 0;
if(viewWidthHalf>viewHeightHalf)
radius=viewHeightHalf-10;
else
radius=viewWidthHalf-10;
circlePaint.setStyle(Style.FILL);
circlePaint.setAntiAlias(true);
//set the paint color using the circle color specified
circlePaint.setColor(color);
canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
//set the text color using the color specified
circlePaint.setColor(color);
//set text properties
circlePaint.setTextAlign(Paint.Align.CENTER);
circlePaint.setTextSize(50);
//draw the text using the string attribute and chosen properties
canvas.drawText(name, viewWidthHalf, viewHeightHalf, circlePaint);
}
}
但我得到一个错误,我的自定义布局没有膨胀。请帮帮我。