【问题标题】:Bottom Sheet not Show All Layout底页不显示所有布局
【发布时间】:2021-07-14 04:51:43
【问题描述】:

如何在底部工作表中显示所有布局 kotlin 例如,我需要像这样显示底页

但是当点击底部表单时,它只显示一半

我是 kotlin 的新手,如何使用显示所有底页,这是我的代码:

点击按钮

var Bottomfragmenjemput = JemputSayaFragment()

        binding.button3.setOnClickListener {
            Bottomfragmenjemput.show(supportFragmentManager,"TAG")
        }

这是我的 BottomsheetFragment.kt

class JemputSayaFragment : BottomSheetDialogFragment() {

    private lateinit var binding : BottomsheetlayoutBinding
    private lateinit var bottomsheet : BottomSheetBehavior<View>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = BottomsheetlayoutBinding.inflate(inflater,container,false)

        binding.numberPicker.minValue = 1
        binding.numberPicker.maxValue = 4


        return binding.root
    }
}

此 XML 代码 bottomsheetlayout.xml :

<?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:padding="16dp"
    app:layout_behavior=""
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jemput Saya"
        style="@style/TextAppearance.MaterialComponents.Headline6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView11"
        style="@style/TextAppearance.MaterialComponents.Caption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="masyarakat berperkara dapat meminta jemput apabila terjadi kendala untuk datang pada hari sidang"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView10" />

    <TextView
        android:id="@+id/textView12"
        style="@style/TextAppearance.MaterialComponents.Body2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Jumlah Penumpang : "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView2" />

    <NumberPicker
        android:id="@+id/numberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView12" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@drawable/circlebutton"
        android:text="Jemput saya"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/numberPicker" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="316dp"
        android:layout_height="198dp"
        android:layout_marginTop="16dp"
        app:layout_constraintDimensionRatio="w,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView11"
        app:srcCompat="@drawable/ic_pick" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 你能添加完整的 abcBottomSheet.java 代码吗?它是 xml 文件。
  • @UmeshYadav 已编辑
  • 添加 MainActivity /fragment 类,您将在底部工作表与 xml 重叠。

标签: android android-studio kotlin


【解决方案1】:

对话框有一个监听器,一旦显示对话框就会触发。如果没有布局,对话框将无法显示。

因此,在模态底部工作表 (BottomSheetFragment) 的 onCreateDialog() 中,就在返回对话框之前(或任何地方,一旦你有对对话框的引用),调用:

// This listener's onShow is fired when the dialog is shown
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {

        // In a previous life I used this method to get handles to the positive and negative buttons
        // of a dialog in order to change their Typeface. Good ol' days.

        BottomSheetDialog d = (BottomSheetDialog) dialog;

        // This is gotten directly from the source of BottomSheetDialog
        // in the wrapInBottomSheet() method
        FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);

        // Right here!
        BottomSheetBehavior.from(bottomSheet)
                .setState(BottomSheetBehavior.STATE_EXPANDED);
    }
});

this answer"Set state of BottomSheetDialogFragment to expanded" 的引用。

【讨论】:

    【解决方案2】:

    您需要在底部工作表行为中设置自己的窥视高度。覆盖 onCreateDialogJemputSayaFragment

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        val peekHeightInPixels = /*your desired height*/
        dialog.behavior.peekHeight = peekHeightInPixels
        return dialog
    }
    

    【讨论】:

    • 这项工作,谢谢你,但使用它会更好dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多