【问题标题】:Issue with recycler view inside constraint layout约束布局内的回收站视图问题
【发布时间】:2018-05-22 22:12:11
【问题描述】:

我正在尝试基于复杂布局创建底部工作表对话框。 我必须实现的是一个带有标题布局的对话框、一个带有元素列表的回收器视图和一个带有几个按钮的底栏。 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Layout injected inside this linear layout container programmatically -->
        <LinearLayout
            android:id="@+id/header_dialog_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@id/form_dialog_recycler_view"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/form_dialog_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toTopOf="@id/bottom_sheet_container_buttons"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/header_dialog_container" />

        <LinearLayout
            android:id="@+id/bottom_sheet_container_buttons"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:id="@+id/dialog_sheet_button_reset"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_reset"
                android:textColor="@color/dangerColor" />

            <Button
                android:id="@+id/dialog_sheet_button_close"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_close" />

        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</layout>

不幸的是,我的回收站视图高度始终为零。我试图将android:layout_height="0dp" 更改为android:layout_height="wrap_content",但正如预期的那样,它位于底部容器下方。我错过了什么吗?

【问题讨论】:

    标签: android android-recyclerview android-linearlayout android-constraintlayout bottom-sheet


    【解决方案1】:

    如果RecyclerView 在ConstraintLayout 内部,请尝试app:layout_constraintHeight_default="wrap"。

    【讨论】:

      【解决方案2】:

      我最终得到了不同的解决方案。我用线性布局更改了约束布局。我设置了底部边距。

      <?xml version="1.0" encoding="utf-8"?>
      <layout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
      
              <LinearLayout
                  android:id="@+id/header_dialog_container"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" />
      
              <android.support.v7.widget.RecyclerView
                  android:id="@+id/form_dialog_recycler_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginBottom="60dp"
                  android:scrollbars="vertical"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
              <LinearLayout
                  android:id="@+id/bottom_sheet_container_buttons"
                  android:layout_width="match_parent"
                  android:layout_height="60dp"
                  android:layout_marginTop="-60dp"
                  android:orientation="horizontal">
      
                  <Button
                      android:id="@+id/dialog_sheet_button_reset"
                      style="@style/ActionSheetTitle"
                      android:layout_width="0dp"
                      android:layout_height="60dp"
                      android:layout_weight="0.5"
                      android:background="@color/bottomBarContainerColor"
                      android:text="@string/general_reset"
                      android:textColor="@color/dangerColor" />
      
                  <Button
                      android:id="@+id/dialog_sheet_button_close"
                      style="@style/ActionSheetTitle"
                      android:layout_width="0dp"
                      android:layout_height="60dp"
                      android:layout_weight="0.5"
                      android:background="@color/bottomBarContainerColor"
                      android:text="@string/general_close" />
      
              </LinearLayout>
          </LinearLayout>
      </layout>
      

      【讨论】:

        【解决方案3】:

        试试这个代码:

        <?xml version="1.0" encoding="utf-8"?>
        <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
        
                <!-- Layout injected inside this linear layout container programmatically -->
                <LinearLayout
                    android:id="@+id/header_dialog_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toTopOf="@id/form_dialog_recycler_view"
                    app:layout_constraintTop_toTopOf="parent" />
        
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/form_dialog_recycler_view"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:scrollbars="vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    app:layout_constraintBottom_toTopOf="@id/bottom_sheet_container_buttons"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/header_dialog_container" />
        
                <LinearLayout
                    android:id="@+id/bottom_sheet_container_buttons"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:orientation="horizontal"
                    app:layout_constraintBottom_toBottomOf="parent">
        
                    <Button
                        android:id="@+id/dialog_sheet_button_reset"
                        style="@style/ActionSheetTitle"
                        android:layout_width="0dp"
                        android:layout_height="60dp"
                        android:layout_weight="0.5"
                        android:background="@color/bottomBarContainerColor"
                        android:text="@string/general_reset"
                        android:textColor="@color/dangerColor" />
        
                    <Button
                        android:id="@+id/dialog_sheet_button_close"
                        style="@style/ActionSheetTitle"
                        android:layout_width="0dp"
                        android:layout_height="60dp"
                        android:layout_weight="0.5"
                        android:background="@color/bottomBarContainerColor"
                        android:text="@string/general_close" />
        
                </LinearLayout>
            </android.support.constraint.ConstraintLayout>
        </layout>
        

        【讨论】:

        • 感谢您的回答。不幸的是,它没有用。
        【解决方案4】:

        在您的RecyclerView 中使用app:layout_constrainedHeight="true"。它将调整内部约束并且不会低于底部容器。

        【讨论】:

          猜你喜欢
          • 2020-06-09
          • 2020-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-20
          • 2019-03-18
          • 1970-01-01
          相关资源
          最近更新 更多