【发布时间】:2015-11-07 12:13:16
【问题描述】:
我有一个包含两个按钮和一个文本视图的相对布局。我想要发生的是在最左边有一个按钮,在中心有一个文本视图,在最右边有另一个按钮。尝试在没有 XML 的情况下执行此操作。
这是我的代码:
RelativeLayout fm = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
fm.setLayoutParams(lp);
fm.setBackgroundColor(Color.CYAN);
Button done = new Button(this);
done.setId(10);
done.setText("Done");
Button save = new Button(this);
save.setId(12);
save.setText("Save");
TextView formManager = new TextView(this);
formManager.setId(11);
formManager.setText("Form Manager");
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
fm.addView(formManager, lp);
lp.removeRule(RelativeLayout.CENTER_IN_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
fm.addView(done, lp);
lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
fm.addView(save, lp);
lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mainLayout.addView(fm);
问题是……保存按钮会拉伸并占据整个布局,而且非常薄。基本上,这段代码并没有像我想的那样发生。关于如何实现这一目标的任何想法?
【问题讨论】:
标签: java android android-relativelayout