2012-01-16 92 views
0

我試圖添加一個視圖到佈局,並把視圖放在屏幕的底部。 這裏是我的代碼視圖和佈局vs setGravity

RelativeLayout game = new RelativeLayout(this); 
game.addView(new Menu(this)); 
game.addView(ad.getAd()); 
game.setGravity(Gravity.BOTTOM); 
setContentView(game); 

但查看被印在屏幕的頂部,爲什麼心不是setGravity工作?

+0

http://stackoverflow.com/questions/2305395/laying-out-views-in-relativelayout-programmatically – 2012-01-16 14:54:29

回答

0

重力在RelativeLayout中不起作用。

試試這個:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(null); 
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1); 
Menu menu = new Menu(); 
menu.setLayoutParams(params); 
ad.getAd().setLayoutParams(params); 
game.addView(menu); 
game.addView(ad.getAd());