3 回答
TA贡献1812条经验 获得超5个赞
您可以在您的 RelativeLayout 内嵌套另一个布局,例如 LinearLayout 或另一个 RelativeLayout,然后将两个兄弟视图放在其中。或者,您可以使用 android:layout_above 在 RelativeLayout 内将两者链接在一起。
TA贡献1817条经验 获得超14个赞
您可以在使用RelativeLayout
.
例如:
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/tv1"/></RelativeLayout>
此示例布局将显示两个TextView
,均与其父项相关居中,并将一个放置在另一个下方。
有许多参数可用于将视图放置在此布局上,如下面的示例来自文档:
可用于 RelativeLayout 中的视图的许多布局属性中的一些包括:
android:layout_alignParentTop
如果为“true”,则使此视图的顶部边缘与父视图的顶部边缘匹配。
android:layout_centerVertical
如果为“true”,则此子项在其父项内垂直居中。
android:layout_below
将此视图的上边缘定位在使用资源 ID 指定的视图下方。
android:layout_toRightOf
将此视图的左边缘定位到使用资源 ID 指定的视图的右侧。
我建议您阅读有关RelativeLayout
检查其工作原理的文档。
现在,最好看看另一个名为 的布局管理器ConstraintLayout
,它有点像RelativeLayout
,但功能更强大。看看这里,看看它是如何工作的。
此外,该站点还展示了如何使用 模仿其他布局行为ConstraintLayout
,请查看!
添加回答
举报