Android:将按钮放在绘制线的顶部

时间:2013-10-20 14:57:35

标签: android android-widget android-canvas android-view

我正在动态创建自定义视图,这是一个使用按钮,图像和线条的交互式流程图。我为每个元素创建一个子视图,然后将它们添加到主视图组。即:

// lines
LineDrawView lineView = new LineDrawView(getActivity(),lineArray);
lineView.setClickable(false);
layout.addView(lineView);

然后我的按钮:

Button button = new Button(getActivity());
button.setText(title);
layout.addView(button, new AbsoluteLayout.LayoutParams(width, height, x, y));

我的所有布局参数都是预定义的,并存储在定义每个流程图的数据库中。除了我的线条,我得到了我想要的效果。在某些情况下,我只绘制一行,我希望按钮位于行顶部。相反,我的路线始终处于最前沿。即:

flowchart button

我的问题是如何强制我的线视图在后台?这是我的行视图代码:

public class LineDrawView extends View {

private int KMULTIPLIER = 2;
private ArrayList<String []> lineArray;

public LineDrawView(Context context, ArrayList<String []> lineArray) {
    super(context);
    this.lineArray = lineArray;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);;
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);

    // determine the screen size & initialize CONSTANT
    int screenSize = getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;

    switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            KMULTIPLIER = 4;
            break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            KMULTIPLIER = 3;
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            KMULTIPLIER = 2;
            break;
        default:
    }

    // draw the lines
    if (lineArray.size() > 0) {
        for (int i=0;i < lineArray.size();i++) {
            String[] array = lineArray.get(i);
            int x = Integer.parseInt(array[0].trim())*KMULTIPLIER;
            int y = Integer.parseInt(array[1].trim())*KMULTIPLIER;
            int x2 = Integer.parseInt(array[2].trim())*KMULTIPLIER;
            int y2 = Integer.parseInt(array[3].trim())*KMULTIPLIER;

            canvas.drawLine(x, y, x2, y2, paint);
        }
    }   
}

}

任何建议都将不胜感激。谢谢你提前。

1 个答案:

答案 0 :(得分:1)

试试这个:     layout.addView(lineView,0);