我被简单的问题困扰了一整天......
在我的 fragment 中,我想从顶部垂直放置 - TextView(title),在其下方 - RecyclerView,在 recyclerview 下方 - 按钮
这是我的 fragment 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"/>
关于android - RecyclerView 与 TextView 和 Button 重叠或消失在 fragment 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117734/