如何设置在code RelativeLayout的布局PARAMS不在XML [英] How to set RelativeLayout layout params in code not in xml

查看:152
本文介绍了如何设置在code RelativeLayout的布局PARAMS不在XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我想补充3按钮在屏幕上的一个对准左一居中,最后一个右对齐

For example I want to add 3 button on screen one align left, one align center, last one align right

我如何设置他们在code的布局,而不是在XML?

How can I set their layout in code, not in XML?

推荐答案

只是一个基本的例子:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2;
button2.setLayoutParams(params);

正如你所看到的,这是你必须做的:

As you can see, this is what you have to do:

  1. 创建一个 RelativeLayout.LayoutParams 对象。
  2. 使用 addRule(INT) addRule(INT,INT)来设置的规则。第一种方法用于添加不需要值的规则。
  3. 设置的参数的视图(在这种情况下,每个按钮)。
  1. Create a RelativeLayout.LayoutParams object.
  2. Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don't require values.
  3. Set the parameters to the view (in this case, to each button).

这篇关于如何设置在code RelativeLayout的布局PARAMS不在XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆