[英]How to make included component always on top
如何在我的搜索组件下方将背景设置为白色(视图布局)? 这是我的代码
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include
layout="@layout/component_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/white"
android:elevation="1dp"/>
</RelativeLayout>
只需像这样更改include
标签和View
标签的顺序:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/white"/>
<include
layout="@layout/component_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
</RelativeLayout>
更新: android:orientation="horizontal"
在我编辑 LinearLayout 以使其成为 RelativeLayout 时出错了。 您不需要在您的 RelativeLayout 中添加它。
如果下面你的意思是在 y 轴上,比如一排又一排,你已经拥有的可能没问题。 在相对布局中一个接一个地放置项目。 你甚至可以通过使用 android:layout_toBottomOf="@+id/your_view_above" 来强制这个位置
现在,如果下面你的意思是在 z 轴上,沿着你的方向从屏幕上下来的那个,(后面)你可能想要按照@amit 建议的那样做,而是使用elevation
。
为包含视图(如search
提供一个id
,
将它的layout_alignParentTop
属性设置为true
并且
将View
的layout_below
属性设置为"@id/search"
:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/search"
layout="@layout/component_search"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
<View
android:layout_below="@id/search"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/white"
android:elevation="1dp"/>
</RelativeLayout>
您不需要此属性android:orientation="horizontal"
,因为它不适用于RelativeLayout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.