android 显示时,底部板材内容未“粘”在顶部

ajsxfq5m  于 2022-12-31  发布在  Android
关注(0)|答案(1)|浏览(130)

我试着按照official bottom sheet documentation来使用它。但是,我遇到了一个非常令人沮丧的问题,正如你从屏幕截图1中看到的,内容的开始和实际底部表单的顶部之间有相当大的空间。当我向上滚动以显示所有内容时,内容"粘"到底部表单的顶部,然后就没事了。参见屏幕截图2。我该如何解决这个问题?
bottomsheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
  android:id="@+id/coordinator"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  
  <FrameLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
    <androidx.constraintlayout.widget.ConstraintLayout
      android:id="@+id/clBottomSheet"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <com.google.android.material.bottomsheet.BottomSheetDragHandleView
        android:id="@+id/drag_handle"
        android:layout_width="match_parent"
        android:layout_height="16dp"
        android:contentDescription="@string/no_qr_code_button"
        app:layout_constraintTop_toTopOf="parent"/>

      <TextView
        android:id="@+id/bottom_sheet_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/section_header_style"
        android:background="@color/background"
        android:text="@string/no_qr_code_button"
        android:textStyle="bold"
        android:layout_below="@+id/drag_handle"
        app:layout_constraintTop_toBottomOf="@+id/drag_handle"
        app:layout_constraintStart_toStartOf="parent"/>
      
      <ImageButton
        android:id="@+id/bottom_sheet_button_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close"
        android:background="@color/background"
        android:layout_toEndOf="@+id/bottom_sheet_title"
        android:layout_above="@+id/tv_no_qr_help_body_1"
        android:contentDescription="@string/no_qr_help_close_button_content_description"
        app:layout_constraintTop_toBottomOf="@id/drag_handle"
        app:layout_constraintEnd_toEndOf="parent"
        android:paddingEnd="@dimen/readid_margin"
        android:paddingTop="@dimen/readid_margin_small"/>

      <TextView
        android:id="@+id/tv_no_qr_help_body_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/text_style"
        android:padding="@dimen/readid_margin"
        android:text="@string/no_qr_help_body_1"
        android:layout_below="@+id/bottom_sheet_title"
        android:background="@color/background"
        app:layout_constraintTop_toBottomOf="@id/bottom_sheet_title"/>
      
      <TextView
        android:id="@+id/tv_no_qr_help_title_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/section_header_style"
        android:text="@string/no_qr_help_title_2"
        android:textStyle="bold"
        android:background="@color/background"
        android:layout_below="@+id/tv_no_qr_help_body_1"
        app:layout_constraintTop_toBottomOf="@id/tv_no_qr_help_body_1"/>
      
      
      <TextView
        android:id="@+id/tv_no_qr_help_body_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/text_style"
        android:padding="@dimen/readid_margin"
        android:text="@string/no_qr_help_body_2"
        android:layout_below="@+id/tv_no_qr_help_title_2"
        app:layout_constraintTop_toBottomOf="@id/tv_no_qr_help_title_2"/>
      
      <TextView
        android:id="@+id/tv_no_qr_help_title_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/section_header_style"
        android:text="@string/no_qr_help_title_3"
        android:textStyle="bold"
        android:background="@color/background"
        android:layout_below="@+id/tv_no_qr_help_body_2"
        app:layout_constraintTop_toBottomOf="@id/tv_no_qr_help_body_2"/>
      
      <TextView
        android:id="@+id/tv_no_qr_help_body_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/text_style"
        android:padding="@dimen/readid_margin"
        android:text="@string/no_qr_help_body_3"
        android:layout_below="@+id/tv_no_qr_help_title_3"
        app:layout_constraintTop_toBottomOf="@id/tv_no_qr_help_title_3"/>
      
    </androidx.constraintlayout.widget.ConstraintLayout>
  </FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

在代码调用中,单击按钮后显示底部工作表:

private fun showNoQrCodeBottomSheet(context: Context) {
    val bottomSheet = BottomSheetDialog(context)
    bottomSheet.setContentView(R.layout.bottomsheet_layout)
    bottomSheet.show()

    val button = bottomSheet.findViewById<ImageButton>(R.id.bottom_sheet_button_close)
    button?.setOnClickListener {
        bottomSheet.dismiss()
    }
}

截图一:

截图二:

6tr1vspr

6tr1vspr1#

请删除此行:-

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

从您的框架布局,并将此线放置在您的父布局CoordinatorLayout
希望这对你有帮助!!

相关问题