2011-05-20 92 views
1

我正在使用shaperawable示例单词(几乎),并且似乎无法在xml中调用shaperawable类。文档说明的唯一额外步骤是覆盖View(Context,AttributeSet),我认为这是我做的。我指的文档在这里:http://developer.android.com/guide/topics/graphics/2d-graphics.html这是我的代码。以XML形式绘制的视图

AndroidTest.java

package com.android.test; 

import android.app.Activity; 
import android.os.Bundle; 

public class AndroidTest extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
} 
} 

ShapeSquare.java

package com.android.test; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.util.AttributeSet; 
import android.view.View; 

public class ShapeSquare extends View { 
private ShapeDrawable mDrawable; 

public ShapeSquare(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    int x = 10; 
    int y = 10; 
    int width = 300; 
    int height = 50; 

    mDrawable = new ShapeDrawable(new OvalShape()); 
    mDrawable.getPaint().setColor(0xff74AC23); 
    mDrawable.setBounds(x, y, x + width, y + height); 
} 

protected void onDraw(Canvas canvas) { 
    mDrawable.draw(canvas); 
} 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<com.android.test.shapedrawable.ShapeSquare 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 

</LinearLayout> 

的错误是强制退出错误,我想不通的地方问题在于。形状属性将由用户输入(最终)决定,因此需要在类中创建形状,而不是所有xml。

+0

plz显示您的logcat输出.. – Sujit 2011-05-20 04:44:13

回答

1

这里找出问题所在。我不得不从

<com.android.test.shapedrawable.ShapeSquare 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 

显然,这只是演示的位置。我认为它以某种方式引用了这个类。