为什么我的回收站视图与布局的一部分重叠

时间:2019-07-18 02:02:45

标签: android android-recyclerview

我有一个版式和一个Recyclerview。布局应该是一个搜索栏,始终与我的recyclerview重叠,如下所示:

enter image description here

但是看起来像这样:

enter image description here

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <LinearLayout
        android:layout_width="match_parent"
        android:foreground="#000"
        android:orientation="horizontal"
        android:id="@+id/searchbar_layout"
        android:layout_height="58dp">
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="3dp"
        android:id="@+id/my_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</android.support.constraint.ConstraintLayout>

我不知道,因为我的回收站视图中只有一小部分与搜索栏重叠,而不是整个。

如何使布局看起来像第一个图像

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <LinearLayout
        android:id="@+id/searchbar_layout"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:foreground="#000"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchbar_layout" />


</android.support.constraint.ConstraintLayout>
  1. 复制此代码
  2. 打开.xml文件
  3. 按CTRL + A选择所有代码
  4. 按下删除按钮
  5. 现在按CTRL + V

问题将得到解决

答案 1 :(得分:0)

默认情况下为sibling views will be drawn in the order they appear in the tree,因此您的RecyclerView将在LinearLayout之后绘制,这导致RecyclerView与LinearLayout重叠。

之所以只有LinearLayout的一部分重叠是因为RecyclerView具有透明的背景。

在LinearLayout将解决此问题之前移动RecyclerView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="3dp"
        android:id="@+id/my_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:foreground="#000"
        android:orientation="horizontal"
        android:id="@+id/searchbar_layout"
        android:layout_height="58dp">
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

修改Z顺序的另一种方法是使用android:elevation,如果您meet some conditions,高程也会给您阴影,但是高程仅适用于Android 5.0及更高版本。

注意:您应该给ConstraintLayout的每个子对象一个约束,就像@moon一样。 要了解有关ConstraintLayout的更多信息,请访问此处:https://developer.android.com/training/constraint-layout