XML中的Shape Drawable视图

时间:2011-05-20 03:42:56

标签: android xml view

我正在逐字逐句地使用shaperawable示例,并且似乎无法在xml中调用shaperawable类。文档中说明的唯一额外步骤是覆盖View(Context,AttributeSet),我认为我做了。我所指的文档在这里:http://developer.android.com/guide/topics/graphics/2d-graphics.html这是我的代码。

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。

1 个答案:

答案 0 :(得分:1)

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

中删除“shapedrawable”
<com.android.test.shapedrawable.ShapeSquare
android:layout_width="fill_parent" 
android:layout_height="wrap_content"  
/>

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