【问题标题】:How to make included component always on top如何使包含的组件始终位于顶部
【发布时间】:2019-07-03 11:16:51
【问题描述】:

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

<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 之上,只需在您的 xml 中切换它们的位置
  • 您可以在布局中设置一些高度,其中包含您希望在“TOP”上的元素。

标签: java android xml android-layout layout


【解决方案1】:

如果下面是指在 y 轴上,例如一个接一个,那么你已经拥有的可能没问题。在您的相对布局中一个接一个地放置项目。你甚至可以使用 android:layout_toBottomOf="@+id/your_view_above" 来强制这个位置

现在,如果您所说的下方是指在 z 轴上,即从屏幕下方朝您的方向(后面)向下移动的那个,您可能想要按照 @amit 的建议进行操作,并改用 elevation

【讨论】:

    【解决方案2】:

    只需像这样改变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 中添加它。

    【讨论】:

    • 嗨@birju,我认为他希望视图位于包含的布局下方。你的答案似乎反其道而行之。
    • @FrancislainyCampos 是的,你是对的。我忘记了 &lt;View&gt; 标签的海拔。所以我删除了它,现在我认为它会显示为他想要的一样(搜索组件下方的白色背景(&lt;View&gt; tag))
    • 没问题@birju。谢谢。我只是不确定您的更新是否已显示在这里,因为在我看来它在这里的方式仍然会在视图之后显示包含。但老实说,我认为他在这里寻找的可能是提升,因为在下面他可能是在后面。
    • @FrancislainyCampos 我已经创建了一个示例来解决他的问题,我很确定它有效。但是,也可以按照您的建议使用elevation 来完成。问题是海拔在&lt;include&gt; 标签上不起作用,所以他需要在&lt;View&gt; 标签上设置android:elevation="-1dp"。然后它将按他的意愿工作。但这里的问题是 elevation 属性在棒棒糖之前的设备上不起作用。所以这可能是他问题的最终解决方案。你可以检查我在这里分享的代码的输出:prntscr.com/mixapz(黑色是包含的布局,粉红色是view标签)
    • 酷!谢谢@birju。不知道海拔不适用于棒棒糖前和 -1 hack。 ;)
    【解决方案3】:

    为包含视图提供id,例如search
    将其 layout_alignParentTop 属性设置为 true
    Viewlayout_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

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多