【问题标题】:how to set a button layout_marginTop/layout_marginBottom in runtime?如何在运行时设置按钮 layout_marginTop/layout_marginBottom?
【发布时间】:2013-02-09 22:51:28
【问题描述】:

我正在使用具有 4 个按钮的相对布局(在中心,一个在另一个下方)。
我的问题是调整所有按钮之间的间距,使所有按钮之间的间距相同。

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

最好的方法是什么?

谢谢,
阿米格尔

【问题讨论】:

    标签: android button android-widget runtime


    【解决方案1】:

    您可以通过修改视图的 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);
    

    应用更改

    我也不知道你是如何获得屏幕尺寸的,但这适用于每个 API:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多