所以我有这个活动,看起来像这样:
<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="com.example.jonas.trainingslog1.WorkoutDataActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
...
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/layout">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
如您所见,该活动有一个 RecyclerView,其项目如下所示:
<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"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="@+id/lst"/>
Run Code Online (Sandbox Code Playgroud)
ListViewItem中的高度RecyclerView应该只与它的内容一样高,所以我将其设置为wrap_content。问题是,只有当我将RecyclerViewItem Layout 的高度设置为时match_parent,这才有效,一旦我将其更改为wrap_contentListView 仅显示一个项目。然而我不能保留RecyclerView项目的高度,match_parent因为这样每个项目RecyclerView都有很多未使用的空间。
有人知道如何解决这个问题吗?我不明白 xml 文件的这种行为。我真的很感激帮助。