无法设置 BottomSheetDialog 初始高度
Unable to set BottomSheetDialog initial height
我正在尝试设置对话框必须从其开始的高度窥视,然后用户如果被拖动应该能够展开它,问题是在任何情况下 bottomsheet 初始状态都会占据屏幕的一半。
BottomSheet 如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/varianti_preferite_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:elevation="8dp"
app:behavior_peekHeight="200dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<ImageButton
android:id="@+id/closeButton"
style="@style/Widget.AppCompat.ImageButton"
android:minWidth="75dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/bottom_border_radius"
android:contentDescription="@string/bottom_sheet_close_button"
android:padding="10dp"
android:layout_marginBottom="16dp"
app:srcCompat="@drawable/ic_baseline_close" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/variantiRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"
tools:listitem="@layout/varianti_preferite">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
我尝试过的:
我尝试覆盖 onCreateDialog 并手动设置 HalfExpanded 比率,但没有任何改变:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(dialog1 -> {
BottomSheetDialog d = (BottomSheetDialog) dialog1;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
BottomSheetBehavior<FrameLayout> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setHalfExpandedRatio(0.2f);
bottomSheetBehavior.setFitToContents(false);
});
return dialog;
}
也欢迎 Kotlin 回答。
我已经通过在 BottomSheet 样式中设置 peek 高度解决了这个问题。
所以在style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/bottom_sheet_rounded</item>
<item name="behavior_peekHeight">@dimen/peek_height</item>
</style>
在dimens.xml
<dimen name="peek_height">200dp</dimen>
但问题是它将 peek_height 设置为应用程序的所有 BottomSheets。
对我成功的方法:
- 在
onCreate
中添加 viewTreeObserver.addOnGlobalLayoutListener
以设置您的 BottomSheet 行为:
binding.root.viewTreeObserver.addOnGlobalLayoutListener {
setupBottomSheetBehaviorForView(binding.bottomFragmentContainer)
}
- 设置行为:
private fun setupBottomSheetBehaviorForView(view: FragmentContainerView) {
val behavior = BottomSheetBehavior.from(view)
val screenHeight =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.currentWindowMetrics.bounds.height()
} else {
val metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
metrics.heightPixels
}
val toolbarLocation = IntArray(COORDINATES_ARRAY_SIZE) // was added to avoid overlapping the toolbar
binding.toolbar.getLocationOnScreen(toolbarLocation) // with the bottom sheet (in case of full screen activity)
behavior.apply {
peekHeight = (screenHeight * BOTTOM_SHEET_PEEK_PERCENT).toInt()
isFitToContents = false
halfExpandedRatio = BOTTOM_SHEET_PEEK_PERCENT
expandedOffset = toolbarLocation[1] // it's optional
}
}
其中 BOTTOM_SHEET_PEEK_PERCENT
是 Float const,初始 peek 高度的 40%:
const val BOTTOM_SHEET_PEEK_PERCENT = 0.40f
我也遇到过类似的问题,我的解决方案是设置 layoutParams
希望它也对你有用
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding.apply {
bottomSheetConstraintLayout.layoutParams.height =
resources.displayMetrics.heightPixels
}
}
我正在尝试设置对话框必须从其开始的高度窥视,然后用户如果被拖动应该能够展开它,问题是在任何情况下 bottomsheet 初始状态都会占据屏幕的一半。
BottomSheet 如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/varianti_preferite_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:elevation="8dp"
app:behavior_peekHeight="200dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<ImageButton
android:id="@+id/closeButton"
style="@style/Widget.AppCompat.ImageButton"
android:minWidth="75dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/bottom_border_radius"
android:contentDescription="@string/bottom_sheet_close_button"
android:padding="10dp"
android:layout_marginBottom="16dp"
app:srcCompat="@drawable/ic_baseline_close" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/variantiRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4"
tools:listitem="@layout/varianti_preferite">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
我尝试过的:
我尝试覆盖 onCreateDialog 并手动设置 HalfExpanded 比率,但没有任何改变:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(dialog1 -> {
BottomSheetDialog d = (BottomSheetDialog) dialog1;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
BottomSheetBehavior<FrameLayout> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setHalfExpandedRatio(0.2f);
bottomSheetBehavior.setFitToContents(false);
});
return dialog;
}
也欢迎 Kotlin 回答。
我已经通过在 BottomSheet 样式中设置 peek 高度解决了这个问题。
所以在style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/bottom_sheet_rounded</item>
<item name="behavior_peekHeight">@dimen/peek_height</item>
</style>
在dimens.xml
<dimen name="peek_height">200dp</dimen>
但问题是它将 peek_height 设置为应用程序的所有 BottomSheets。
对我成功的方法:
- 在
onCreate
中添加viewTreeObserver.addOnGlobalLayoutListener
以设置您的 BottomSheet 行为:
binding.root.viewTreeObserver.addOnGlobalLayoutListener {
setupBottomSheetBehaviorForView(binding.bottomFragmentContainer)
}
- 设置行为:
private fun setupBottomSheetBehaviorForView(view: FragmentContainerView) {
val behavior = BottomSheetBehavior.from(view)
val screenHeight =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.currentWindowMetrics.bounds.height()
} else {
val metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
metrics.heightPixels
}
val toolbarLocation = IntArray(COORDINATES_ARRAY_SIZE) // was added to avoid overlapping the toolbar
binding.toolbar.getLocationOnScreen(toolbarLocation) // with the bottom sheet (in case of full screen activity)
behavior.apply {
peekHeight = (screenHeight * BOTTOM_SHEET_PEEK_PERCENT).toInt()
isFitToContents = false
halfExpandedRatio = BOTTOM_SHEET_PEEK_PERCENT
expandedOffset = toolbarLocation[1] // it's optional
}
}
其中 BOTTOM_SHEET_PEEK_PERCENT
是 Float const,初始 peek 高度的 40%:
const val BOTTOM_SHEET_PEEK_PERCENT = 0.40f
我也遇到过类似的问题,我的解决方案是设置 layoutParams
希望它也对你有用
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding.apply {
bottomSheetConstraintLayout.layoutParams.height =
resources.displayMetrics.heightPixels
}
}