繁体   English   中英

如何将两个视图放置在水平布局中,以便一个视图与左侧对齐,另一个视图与右侧对齐?

[英]How do you put two Views in a horizontal Layout so that one is aligned to the left and the other one - to the right?

假设您要在水平方向的布局中拥有一个TextView和一个Button。 您可以将TextView的左侧对齐,将Button的右侧对齐吗?

---------------------
TextView       Button
---------------------

1)在一个上使用RelativeLayout和android:layout_alignParentLeft =“ true”,在另一个上使用android:layout_alignParentRight =“ true”。

2)将LinearLayout与android:orientation =“ horizo​​ntal”结合使用,并在其中设置android:gravity =“ left”和android:layout_weight =“ 1”,并在android:gravity =“ right”和android:layout_weight =“ 1”上设置其他(是的,需要重量!)

请注意,使用layout_toRightOf将提供左/右相对位置,但不会将其强制到屏幕/父容器的边缘。

使用相对布局,然后将第二个视图定位在右侧。

android:layout_toRightOf="@id/firstView"
android:layout_alignTop="@id/firstView"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM