Android中的自定义视图不充气

时间:2013-08-03 12:54:58

标签: 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);

}


} 

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

1 个答案:

答案 0 :(得分: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);

        }

    }
    }

快照

enter image description here