【问题标题】:why recycler view max height doesn't work? Also i tried recycler view add inside constraint layout but it doesn't work为什么回收站视图最大高度不起作用?我也尝试过在约束布局中添加回收站视图,但它不起作用
【发布时间】:2019-03-08 06:38:28
【问题描述】:

我想达到回收站视图的最大高度,但对我不起作用。我找到了一些答案,但它们对我不起作用。
我尝试了很多方法来解决这个问题。但我找不到答案。这是 XML 代码。任何人都可以帮助我解决这个问题

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewItems"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:maxHeight="100dp"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="100dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • @user1506104 我知道这个问题已经在这里问过了。但是他们的解决方案对我不起作用。这就是为什么再次发布这个问题。很抱歉

标签: android android-layout android-recyclerview recyclerview-layout


【解决方案1】:

将此代码粘贴到您的 xml 活动文件中。希望这会奏效。

 <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewItems"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:maxHeight="100dp"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_default="wrap"
                app:layout_constraintHeight_max="100dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

【讨论】:

    【解决方案2】:

    您的问题对我来说不是很清楚,但是如果您希望 recyclerView 占用所有屏幕空间,您应该使用它:

      <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewItems"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    如果您想限制您的 recyclerview 高度,我不建议在 dp 中使用固定大小 - 使用指南/障碍使您的 recyclerView 仅占屏幕的一部分,例如,如果您希望您的视图仅占屏幕的 30%屏幕使用指南有 30% 的不同,并将您的视图限制在它们上,如下所示:

       <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.4" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.1" />
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewItems"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline2" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 我想在对话框中添加回收站视图。我使用回收站视图高度作为包装内容。当很多项目的对话框高度如此之大时。当回收站视图高度超过 400dp(例如)时,应该停止(我的意思是高度)。然后其他要查看的项目向下滚动。但我在回收站视图中使用了最大宽度,但它不起作用。我还创建了一个约束布局并在其中添加回收器视图并将高度设置为 0dp 。它没有用
    • 这不是一个好习惯(固定尺寸),我的回答有什么问题?为什么不像我给出的示例那样使用带有指南的约束布局?
    • 没用,兄弟。 (我在对话框中使用了回收站视图)这可能是这种方法不起作用的原因......
    • 好的,在这种情况下,使用自定义布局创建自定义对话框,并在您的布局中尝试按照我在使用指南的回答中所要求的那样做。这应该可以工作
    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2021-11-02
    • 2020-06-09
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多