繁体   English   中英

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

[英]How to relate two views of RelativeLayout in Android Studio?

我使用RelativeLayout创建了一个项目,并且发现我的大多数视图都与父级相关。

我只想关联两个相应的视图,例如“对下面的电视剧进行评级”和评级栏(以及父级)。

现有项目中是否有可能?还是我必须重新做一遍?

在此处输入图片说明

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

为了在RelativeLayout中关联两个视图,可以在元素的布局中声明它。

例如,在评分栏的XML布局集中: android:layout_below="@id/rating_textview"
(rating_textview是您的textview的ID)

更多选项: https : //developer.android.com/reference/android/widget/RelativeLayout.LayoutParams

您可以在使用RelativeLayout通过其ID关联和定位任何视图。

例如:

<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 ,两个TextView居中位置与其父级有关,一个位于另一个的下方。

有许多参数可用于将视图放置在此布局上,如以下示例摘自docs:

RelativeLayout中的视图可用的许多布局属性包括:

  • android:layout_alignParentTop如果为“ true”,则使该视图的顶部边缘与父视图的顶部边缘匹配。
  • android:layout_centerVertical如果为“ true”,则将此子项在其父项中垂直居中。
  • android:layout_below将此视图的上边缘置于使用资源ID指定的视图的下方。
  • android:layout_toRightOf将此视图的左边缘定位在使用资源ID指定的视图的右侧。

您可以在此处阅读有关这些参数的更多信息。

我请您阅读有关RelativeLayout 的文档 ,以检查其工作原理。

现在,最好看看另一个名为ConstraintLayout布局管理器,它有点像RelativeLayout ,但是功能更强大。 在这里看看它是如何工作的。

此外, 此站点还显示了如何使用ConstraintLayout模仿其他布局行为,请查看!

暂无
暂无

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

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