为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android Studio 中关联RelativeLayout 的两个视图?

如何在Android Studio 中关联RelativeLayout 的两个视图?

SMILET 2021-08-19 19:16:32
我使用创建了一个项目RelativeLayout,我发现我的大部分视图都与父级相关。我想要的只是将两个相应的视图关联起来,例如“评价下面的电视剧”和评级栏(以及父级)。在现有项目中是否可行,还是我必须重新做一遍?
查看完整描述

3 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

您可以在您的 RelativeLayout 内嵌套另一个布局,例如 LinearLayout 或另一个 RelativeLayout,然后将两个兄弟视图放在其中。或者,您可以使用 android:layout_above 在 RelativeLayout 内将两者链接在一起。


查看完整回答
反对 回复 2021-08-19
?
大话西游666

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,请查看!


查看完整回答
反对 回复 2021-08-19
  • 3 回答
  • 0 关注
  • 412 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信