2011-10-27 41 views
0

我需要顯示xml文件的佈局,並在其上繪製一些基於矢量的圖像。如果執行下面的代碼,則會顯示所有內容,但會顯示佈局。爲什麼?我做錯了什麼?無法使用LayoutInfalter呈現xml佈局。我做錯了什麼?

public class About extends Activity 
{ 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.about); 
    setContentView(new GraphicsView(this)); 
} 

public class GraphicsView extends View 
{ 
    public GraphicsView(Context context) 
    { 
     super(context);     
    } 
    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     Context context; 
     context=getContext(); 

     View aview; 

     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
     aview = inflater.inflate(R.layout.about, null);     

     aview.draw(canvas); 

     Path circle = new Path(); 
     Paint cPaint = new Paint(); 
     cPaint.setColor(Color.LTGRAY); 
     circle.addCircle(150, 150, 100, Direction.CW); 
     canvas.drawPath(circle, cPaint); 
     cPaint.setColor(Color.RED); 
     canvas.drawTextOnPath("Some stupid text", circle, 0, 20, cPaint); 
    } 
} 

}

回答

0

充氣你需要測量它,攤開來給它一個尺寸,因此可以得出正確的觀後。在調用aview.draw()之前,應該調用aview.measure()和aview.layout()。

請注意,您應該永遠不要膨脹來自onDraw()的意見。這是非常昂貴和浪費的。同樣,你應該永遠不要在onDraw()內創建繪畫或路徑。

+0

tahnks,羅曼蓋伊 – alxlabs

0

有一個是從另一個論壇另一個答案:

的FrameLayout:

public class About extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.about); 

     GraphicsView drawing = new GraphicsView(this); 

     FrameLayout container = (FrameLayout) findViewById(R.id.container); 
     container.addView(drawing); 
    } 

    public class GraphicsView extends View 
    { 
     public GraphicsView(Context context) 
     { 
      super(context);     
     } 
     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      Path circle = new Path(); 
      Paint cPaint = new Paint(); 
      cPaint.setColor(Color.LTGRAY); 
      circle.addCircle(150, 150, 100, Direction.CW); 
      canvas.drawPath(circle, cPaint); 
      cPaint.setColor(Color.RED); 
      canvas.drawTextOnPath("Some stupid text", circle, 0, 20, cPaint); 
     } 
    } 
} 

然後在這裏談到XML:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dip" 
    android:id="@+id/container"> 
    <TextView 
     android:id="@+id/about_content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/about_text" /> 
</FrameLayout>