下面是我的自定義視圖的代碼。我基本上是在創建一個圈子並在其中添加一些文本。即使我已經重載兩個構造函數,我得到的佈局沒有膨脹的錯誤。Android中的自定義視圖不充氣
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);
}
}
但我得到一個錯誤,我的自定義佈局不充氣。請幫助我。
什麼錯誤?你如何膨脹你的自定義佈局。發佈代碼? – Raghunandan
錯誤膨脹我的自定義類 –
發佈堆棧跟蹤併發布一些更多相關信息。 – Raghunandan