2013-02-24 39 views
1

我正在使用4个按钮(在中间,一个在另一个下面)的相对布局。
我的问题是调整所有按钮之间的差距,所以它会在所有按钮之间相同。如何在运行时设置按钮layout_marginTop/layout_marginBottom?

我想根据屏幕的高度动态地做到这一点(我使用Display类来获取实际高度)。

什么是最好的办法呢?

感谢,
Amigal

回答

4

你可以通过修改你的视图的的LayoutParams做到这一点

Button b;// your button 
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams(); 
lp.leftMargin=10;//your left margin here 
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to 

有时你需要调用

b.setLayoutParams(lp); 

有变化应用

我也不知道ho你得到的屏幕尺寸,但这在每个API的作品:

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
+0

工作很棒。谢谢你的完整答案 – amigal 2013-02-25 22:30:57