Android:在布局中的视图上以编程方式绘制形状

时间:2012-06-02 22:55:30

标签: android layout dynamic view shape

关于此的文档确实令人困惑。我只需要将rectangels添加到我在main.xml布局文件中定义的View中。它将是布局的一小部分。

我想要实现的是,我想将架子添加到房间,但由于房间形状和架子发生变化,我需要以编程方式添加它们。

以下是我的main.xml文件的一小部分,您可以看到我定义的视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="600dp"
    android:layout_height="650dp"
    android:layout_marginTop="70dp"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/relativeLayout1" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/getDirections"
        android:textSize="20dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/roomplan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:layout_below="@+id/textView3"
        android:layout_marginTop="40dp"
        android:background="@android:color/white" />

</RelativeLayout>

这是我为处理动态更改而创建的自定义View类:

public class CustomView extends View {
    ShapeDrawable roomFrame;
    ArrayList<ShapeDrawable> shelfFrames;

    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        roomFrame.draw(canvas);
        for (ShapeDrawable shelfFrame : shelfFrames){
            shelfFrame.draw(canvas);
        }
    }

    public void setRoom(Stage stage){
        roomFrame = new ShapeDrawable(new RectShape());
        roomFrame.getPaint().setColor(0xff74AC23);   
        roomFrame.setBounds(10, 10, stage.getWidth(), stage.getHeight());
    }

    public void setShelves(ArrayList<Shelf> shelves){
        shelfFrames = new ArrayList<ShapeDrawable>();
        for(int i = 0; i<shelves.size(); i++){
            ShapeDrawable shelfFrame = new ShapeDrawable(new RectShape());
            shelfFrame.getPaint().setColor(0xff74AC23);
            shelfFrame.setBounds(shelves.get(i).getXPosition(), shelves.get(i).getYPosition(), shelves.get(i).getWidth(), shelves.get(i).getHeight());
            shelfFrames.add(shelfFrame);
        }
    }
}

现在简单地说,当我问一个新的房间计划时,我试图将这个类分配给xml布局中的View对象:

public void loadRoomPlan(Room room, ArrayList<Shelf> shelves){
    CustomView asdsView = (CustomView)findViewById(R.id.roomplan);
    asdsView.setRoom(room);
    asdsView.setShelves(shelves);
    asdsView.invalidate();
}

我总是得到

  

引起:java.lang.ClassCastException:android.view.View无法强制转换为org.example.myproject.CustomView

错误。

可能我这样做非常错误,不是吗?

1 个答案:

答案 0 :(得分:0)

错误似乎在这一行:

CustomView asdsView = (CustomView)findViewById(R.id.shopplan);

什么是shopplan?如果这是一个错误,您的意思是R.id.roomplan尝试在您的自定义视图的布局中替换视图:

<org.example.myproject.CustomView
    android:id="@+id/roomplan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="40dp"
    android:background="@android:color/white" />

<强>更新

尝试将其他两个构造函数添加到CustomView类:

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

在xml布局中使用自定义视图时,视图必须处理布局属性(构造函数AttributeSet参数)。