android 打开时无法将BottomSheetDialogFragment扩展到屏幕顶部

nukf8bse  于 2023-05-05  发布在  Android
关注(0)|答案(3)|浏览(207)

我无法将底部的shee对话框片段扩展到屏幕顶部。如果我将layout_height(用于测试)设置为某个较低的值,它总是占屏幕的60%或更少。我已经尝试将其设置为600,700,800(目前为900dp),它仍然没有高于下面的截图(有4个textview只是为了测试,它们在屏幕上不可见):
60% bottom sheet
我的底部工作表片段XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayotCoordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_gravity="fill_vertical"
    android:fitsSystemWindows="true"
    app:layout_anchorGravity="top">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/mainLayoutConstraint"
        android:layout_width="match_parent"
        android:layout_height="900dp"
        android:fitsSystemWindows="false"
        app:layout_anchorGravity="top"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <com.safetyheads.akademiaandroida.MyTest2View
            android:id="@+id/myTest2View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:hint="type something"
            app:layout_constraintBottom_toTopOf="@+id/textView4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:name="First nameno"
            app:visibility="0">

        </com.safetyheads.akademiaandroida.MyTest2View>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myTest2View" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView5" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView7" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的底部工作表对话框片段类:

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.Nullable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.safetyheads.akademiaandroida.databinding.FragmentBottomSheetWriteToUsBinding

class BottomSheetWriteToUsFragment :BottomSheetDialogFragment() {

    private lateinit var binding: FragmentBottomSheetWriteToUsBinding

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

    @SuppressLint("ResourceType")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        bottomSheetDialog.setOnShowListener { dialog: DialogInterface ->
            val bottomSheet = (dialog as BottomSheetDialog).findViewById<ConstraintLayout>(R.id.mainLayoutConstraint)
            if (bottomSheet != null) BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED
        }
        return bottomSheetDialog
    }
}

有一些代码从一个解决方案,我发现与bottomsheetbehavior.state_expanded,但它不是使我的bottom表去顶部,因为我的预期。
我可以手动拖动它,然后它是在全屏。
有没有人,请帮助我,使底部工作表对话框片段去到屏幕的顶部(如fulscreen)自动打开后?

nxowjjhe

nxowjjhe1#

这是我的代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="15dp"
        android:background="@drawable/bottom_sheet_background"
        >
    ...
    </RelativeLayout>
</RelativeLayout>

private void showBottomSheetDialog() {
    bottomSheetDialog = new BottomSheetDialog(activity);
    bottomSheetDialog.setContentView(R.layout.pickup_bottom_sheet_layout);
    ...
    bottomSheetDialog.show();
}
2w3kk1z5

2w3kk1z52#

禁用对话框调整内容行为。另外,我会使用onCreate而不是setOnShowListener的lambda来做这些事情。

class BottomSheetWriteToUsFragment : BottomSheetDialogFragment() {

    private lateinit var binding: FragmentBottomSheetWriteToUsBinding

    private val behavior by lazy { (dialog as BottomSheetDialog).behavior }

    override fun onStart() {
        super.onStart()

        behavior.isFitToContents = false
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

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

}
pcww981p

pcww981p3#

@RusArtM我已经做了这些变化,就像在你的帖子,现在我的底部工作表看起来像下面的截图:
bottom sheet after changes
看起来主要的布局是在全屏上,但内容仍然在中间。我可以手动将内容(myTest 2 View)滑动到顶部。

相关问题