我正在拼湊一個帶有 3 個垂直堆疊的控制元件的螢屏。具體來說,一個 RecyclerView 夾在兩個 TextView 之間。我有兩種情況要解決。
- 當 RecyclerView 中顯示的專案很少時,應將 3 個專案打包到螢屏頂部,并在它們下方留有空白。
- 當 RecyclerView 中顯示許多專案時,將使用全屏,最后一個 TextView 位于螢屏底部并在 RecyclerView 中心滾動。
我正在努力讓最后一個 TextView 的位置正確,因為如果 RecyclerView 中有很多專案,它要么被推離螢屏,要么被埋在 RecyclerView 后面。我需要的是一種方法來告訴 RecyclerView 包裝內容,但當它開始將 TextView 推離螢屏時停止增長。
下面是我的布局,有什么建議嗎?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:id="@ id/topItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@ id/middleItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/middleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@ id/bottomItem"
app:layout_constraintTop_toBottomOf="@ id/topItem" />
<TextView
android:id="@ id/bottomItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@ id/middleItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
嘗試以下操作:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/topItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the top item."
app:layout_constraintBottom_toTopOf="@ id/middleItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/middleItem"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="none"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@ id/bottomItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/topItem"
tools:itemCount="3" />
<TextView
android:id="@ id/bottomItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="This is the bottom item."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/middleItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
在RecyclerView中包含三個專案:

RecyclerView中有 50 個專案:

主要變化是:
- 添加
app:layout_constraintVertical_bias="0.0"到頂部TextView以將鏈始終定位在頂部; - 添加
app:layout_constrainedHeight="true"到RecyclerView以確保視圖不會離開螢屏; - 將match_parent更改為
0dp并設定 ConstraintLayout 的直接子級的水平約束。 - 將RecyclerView 的高度更改為“0dp”。
更新:雖然上述方法有效,但有點過頭了。如評論中所述,只需將RecyclerView的高度設定為0dp.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/topItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the top item."
app:layout_constraintBottom_toTopOf="@ id/middleItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/middleItem"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@ id/bottomItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/topItem"
tools:itemCount="3" />
<TextView
android:id="@ id/bottomItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="This is the bottom item."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/middleItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/460525.html
下一篇:如何按活動添加TabItem?
