如何以编程方式在RelativeLayout中布局视图?
我试图以编程方式实现以下目标(而不是通过 XML 声明方式):
<RelativeLayout...>
<TextView ...
android:id="@+id/label1" />
<TextView ...
android:id="@+id/label2"
android:layout_below: "@id/label1" />
</RelativeLayout>
换句话说,如何使第二个 TextView 出现在第一个 TextView 下方,但我想用代码来实现:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
更新:
谢谢,TreeUK。我明白了大体方向,但它仍然不起作用 - “B”与“A”重叠。我做错了什么?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
I'm trying to achieve the following programmatically (rather than declaratively via XML):
<RelativeLayout...>
<TextView ...
android:id="@+id/label1" />
<TextView ...
android:id="@+id/label2"
android:layout_below: "@id/label1" />
</RelativeLayout>
In other words, how do I make the second TextView
appear below the first one, but I want to do it in code:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
Update:
Thanks, TreeUK. I understand the general direction, but it still doesn't work - "B" overlaps "A". What am I doing wrong?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
根据我的了解,您必须使用 LayoutParams 添加视图。
所有功劳都归功于 sechastain,要以编程方式相对定位您的项目,您必须为它们分配 id。
然后
addRule(RelativeLayout.RIGHT_OF, tv1.getId());
From what I've been able to piece together, you have to add the view using LayoutParams.
All credit to sechastain, to relatively position your items programmatically you have to assign ids to them.
Then
addRule(RelativeLayout.RIGHT_OF, tv1.getId());
长话短说:
使用相对布局,您可以在布局内定位元素。
创建一个新的RelativeLayout.LayoutParams
(无论什么...填充父级或包装内容,绝对数字(如果必须),或引用 XML 资源)
添加规则:
规则涉及父级或层次结构中的其他“兄弟”。
只需应用布局参数:最“健康”的方法是:
注意:不要通过布局回调更改布局。这样做很诱人,因为这是视图获得实际大小的时候。然而,在这种情况下,预计会出现意想不到的结果。
Cut the long story short:
With relative layout you position elements inside the layout.
create a new RelativeLayout.LayoutParams
(whatever... fill parent or wrap content, absolute numbers if you must, or reference to an XML resource)
Add rules:
Rules refer to the parent or to other "brothers" in the hierarchy.
Just apply the layout params: The most 'healthy' way to do that is:
Watch out: Don't change layout from the layout callbacks. It is tempting to do so because this is when views get their actual sizes. However, in that case, unexpected results are expected.
刚刚花了4个小时解决这个问题。最后意识到您不能使用零作为视图ID。你可能会认为 NO_ID == -1 是允许的,但如果你把它交给你的观点,事情往往会变得混乱......
Just spent 4 hours with this problem. Finally realized that you must not use zero as view id. You would think that it is allowed as NO_ID == -1, but things tend to go haywire if you give it to your view...
Android 22 最小可运行示例
来源:
适用于
android create project ...
生成的默认项目。 具有最少构建代码的 GitHub 存储库。Android 22 minimal runnable example
Source:
Works with the default project generated by
android create project ...
. GitHub repository with minimal build code.打电话
之后
call
after
尝试:
Try:
这种使用 ViewGroup.MarginLayoutParams 的方法对我有用:
This approach with ViewGroup.MarginLayoutParams worked for me:
如果您确实想手动布局,我建议根本不要使用标准布局。全部自己完成,这里有一个 kotlin 示例:
这可能会让您了解这是如何工作的
If you really want to layout manually, i'd suggest not to use a standard layout at all. Do it all on your own, here a kotlin example:
This might give you an idea how this could work