2013-08-03 81 views
1

下面是我的自定義視圖的代碼。我基本上是在創建一個圈子並在其中添加一些文本。即使我已經重載兩個構造函數,我得到的佈局沒有膨脹的錯誤。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); 

} 


} 

但我得到一個錯誤,我的自定義佈局不充氣。請幫助我。

+0

什麼錯誤?你如何膨脹你的自定義佈局。發佈代碼? – Raghunandan

+0

錯誤膨脹我的自定義類 –

+0

發佈堆棧跟蹤併發布一些更多相關信息。 – Raghunandan

回答

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

+0

我在MainActivity中有一個字符串。我如何在圓圈頂部顯示此字符串? –

+0

您必須將字符串傳遞給自定義視圖構造函數並在那裏使用它。如果你發佈更多的代碼或信息,那麼現在我只能假設/想象會更好 – Raghunandan

相關問題