2010-04-28 88 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的類糖的方法。