[英]How to relate two views of RelativeLayout in Android Studio?
您可以将另一个布局嵌套在您的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.