0
我正在動態創建一個自定義視圖,該視圖是使用按鈕,圖像和線條的交互式流程圖。我爲每個元素創建一個子視圖,然後將它們添加到主視圖組中。即:Android:在繪製線上放置按鈕
// 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));
所有我的佈局參數是預定義的,並存儲在其中定義了每個流程圖的數據庫。我得到了我想要的效果,但我的線條除外。在某些情況下,我只畫了一條線,我希望按鈕位於最上方。相反,我的路線始終處於最前沿。即:
我的問題是我怎麼逼我行以在後臺?這裏是我行的視圖代碼:
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);
}
}
}
}
任何建議,將不勝感激。先謝謝了。
感謝您的回覆!我認爲我已經嘗試過,但現在起作用了。唯一的區別是,在我先添加行並將其設置爲0.現在,我最後添加行並將其設置爲0。 – wyoskibum