Java 如何使包含的组件始终位于顶部

Java 如何使包含的组件始终位于顶部,java,android,xml,android-layout,layout,Java,Android,Xml,Android Layout,Layout,如何在我的搜索组件下将背景设置为白色(视图布局)?这是我的密码 只需更改包含标记和查看标记的顺序,如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="match_parent" android:layout_height=

如何在我的搜索组件下将背景设置为白色(视图布局)?这是我的密码



只需更改
包含
标记和
查看
标记的顺序,如下所示:

<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以使其相对年轻。你不需要在你的相对论中加上它。

如果你所说的“下面”是指y轴,比如一行接一行,那么你已经拥有的可能就没问题了。在相对布局中一个接一个地放置项目。您甚至可以通过使用android:layout_toBottomOf=“@+id/your_view_over”强制此位置


现在,如果你所说的“下方”是指在z轴上,沿着你的方向(后面)从屏幕上下来的那一个,你可能想按照@amit的建议,使用
仰角

给include视图一个
id
,比如
搜索
将其
layout\u alignParentTop
属性设置为
true

视图
下面的
布局属性设置为
“@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>


您不需要此属性,因为它不适用于
相对视图
如果您希望
包含的
位于
视图的顶部
只需切换它们在XML中的位置即可。您可以在布局中设置一些高程,其中包含希望位于视图上的元素“顶部”。嗨@birju,我想他希望视图位于包含的布局下方。你的答案似乎是相反的。@FrancislainyCampos是的,你是对的。我忘记了
标记上应用了标高。所以我删除了它,现在我想它将显示为他想要的样子(白色背景(
标记
)(位于搜索组件下方)没问题@birju。谢谢。我只是不确定你的更新是否已经显示在这里,因为在我看来,它仍然会在视图后显示include。但老实说,我认为这可能是他在这里寻找的高度,因为下面他可能是指后面的高度。@FrancislainyCampos我创建了一个示例来解决这个问题s的问题,我很确定它是有效的。但是,也可以按照您的建议使用
提升
。问题是提升在
标记上不起作用,所以他需要设置
android:elevation=“-1dp“
标签上。然后它就会按照他想要的那样工作。但这里的问题是,
elevation
属性在棒棒糖前的设备上不起作用。因此,这可能是他的问题的最终解决方案。您可以检查我在这里共享的代码的输出:(黑色是包含的布局,粉色是
视图
标记)酷!谢谢@birju。我不知道elevation在棒棒糖前是不起作用的,就像-1黑客一样