2010-04-28 90 views
0

我试图构建一个android应用程序,其特点是在RelativeLayout中绘制的图形显示。我想在画布上不同点处绘制的几个参数旁边放置“+”和“ - ”按钮。这些职位是自由形式的,似乎不符合任何标准的XML布局。在RelativeLayout上的任意位置的Android按钮

我知道如何以编程方式创建按钮,但我不知道如何将它们放在需要它们的画布上。我假设在绘制完所有图形后,这将在视图线程的doDraw()方法中完成,但是如何?

回答

0

我挣扎于同样的问题,并发现了很大solution

RelativeLayout规则,如“leftOf”或“rightOf”可以通过编程来实现这样的:

RelativeLayout container = new RelativeLayout(getApplicationContext()); 

Button weight = new Button(getApplicationContext()); 
final int WEIGHT_ID = 0; 
weight.setId(WEIGHT_ID); 
weight.setText("0.0"); 
LayoutParams wrapBoth = 
    new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
container.addView(weight, wrapBoth); 

Button increaseWeight = new Button(getApplicationContext()); 
increaseWeight.setText("+"); 
// Note the difference: RelativeLayout.LayoutParams in spite of LayoutParams 
RelativeLayout.LayoutParams toBeRightOfWeight = 
    new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
container.addView(parameter,wrapBoth); 
// Sweet part 
clearAirParams.addRule(RelativeLayout.RIGHT_OF, WEIGHT_ID); 
container.addView(increaseWeight, toBeRightOfWeight); 

所以,在代码中你可以创建一个“容器” RelativeLayout,然后用唯一的ID添加几个View S和,最后,创建RelativeLayout.LayoutParams对象来实现类似于XML的类糖的方法。