1

下面是我的自定义视图代码。我基本上是在创建一个圆圈并将一些文字放入其中。即使我已经重载了两个构造函数,我也得到了布局没有膨胀的错误。

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);

}


} 

但我得到一个错误,我的自定义布局没有膨胀。请帮帮我。

4

1 回答 1

0

尝试这个。我只做了一些小的改动。改变了颜色和文字。有用。从发布的代码中,我看不出代码有什么错误。

代码 :

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        PieChart rv = new PieChart(this);
       // rv.setOnTouchListener(this);
        setContentView(rv);

    }
    public class PieChart extends View {

          public PieChart(Context context)
          {
             super(context);

          }

         @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.BLUE);

          canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);

          //set the text color using the color specified
          circlePaint.setColor(Color.GREEN);

          //set text properties
          circlePaint.setTextAlign(Paint.Align.CENTER);
          circlePaint.setTextSize(50);

          //draw the text using the string attribute and chosen properties
          canvas.drawText("Raghunandan", viewWidthHalf, viewHeightHalf, circlePaint);

        }

    }
    }

快照

在此处输入图像描述

于 2013-08-03T13:15:01.470 回答