自定义视图中的自定义视图

时间:2012-03-26 13:52:38

标签: android android-layout

我有一个自定义视图,它扩展了RelativeLayout。 此相对布局应包含扩展视图的矩形,此矩形应由位图绘制并在其中包含TextView ..

但我不能将这个矩形添加到RelativeLayout,以便它们彼此低于......看起来LayoutParams似乎不起作用..你能帮帮我吗?

自定义视图扩展了RelativeLayout

public class DrawView extends RelativeLayout {
    private Context context;

    public DrawView(Context context, ArrayList<String> dragFieldText, ArrayList<String> targetFieldText, int height, int width) {
        super(context);
        this.context = context;

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

        Rectangle.count = 1;
        for (int i = 0; i < targetFieldText.size(); i++) {

            Rectangle targetRectangle = new Rectangle(context);         
            this.addView(targetRectangle);
        }

        Rectangle tmpRect = null;

        for (int i = 0; i < dragFieldText.size(); i++) {

            Rectangle dragRectangle = new Rectangle(context);

            if(tmpRect != null){
                tmpRect.setId(i);
                lp.addRule(RelativeLayout.BELOW, tmpRect.getId());
                dragRectangle.setLayoutParams(lp);          
            } 

            this.addView(dragRectangle);        
            tmpRect = dragRectangle;

        }       

    }

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = View.MeasureSpec.getSize(widthMeasureSpec);
        int height = View.MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

矩形扩展视图

public class Rectangle extends View{

    public Rectangle(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(getSomeBitmapImg(), new Matrix(), null);


    }

0 个答案:

没有答案