RecyclerView 与 TextView 和 Button 重叠或消失在片段内

RecyclerView overlaps TextView and Button or disappear inside the fragment

我被一个简单的问题困了一整天...
在我的片段中,我想从顶部垂直放置 - TextView(title),在它下面 - RecyclerView,在 recyclerview 下面 - 按钮 这是我的片段xml

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

<TextView
    android:id="@+id/country_output"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/recycler_view_title"
    android:textAlignment="center"
    android:textSize="30sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    app:layout_constraintBottom_toTopOf="@id/btn_country_choice"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/country_output"
    />

<Button
    android:id="@+id/btn_country_choice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/yes"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

在这种情况下,recyclerview 与 textview 重叠,而按钮与 recyclerview 部分重叠。这是截图

如果我在 recyclerview 中定义 android:layout_height="0dp" 那么我有另一个问题 - recyclerview 消失了

android studio 中的预览显示我需要的一切,但在设备上一切都是另一回事

有什么建议吗?

您的 RecyclerView 存在限制问题。它的高度不受约束,它正在包裹它的内容。你应该使用 0dp,这意味着 MATCH_CONSTRAINT:

Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

https://developer.android.com/reference/android/support/constraint/ConstraintLayout

另外,使用 @+id 而不是 @id。这是因为您的 Button ID 仅在布局中的 RecyclerView 之前声明。来自文档:

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace.

所以,应用一切:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="12dp"
    app:layout_constraintBottom_toTopOf="@+id/btn_country_choice"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/country_output"/>

使用 MATCH_CONSTRAINT 作为 RecyclerView 的高度和宽度,添加到有问题的布局代码中。如下

android:layout_width="0dp"
android:layout_height="0dp"