Android BottomSheetDialogFragment 没有完全展开
Android BottomSheetDialogFragment does not expand completely
我有以下测试底部 sheet 实现。
当我将 peekHeight 设置为小于 500 的值时,它起作用了。在某个值之后,任何 peek 高度的增加都不会改变底部 sheet 的扩展方式。它只是保留在那里只能手动拖动。我们如何以编程方式设置 peekHeight 以确保底部 sheet 自动扩展到 peek 高度。
bottom_sheet_dialog_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/locUXCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/locUXView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="1 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="2 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="3 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="4 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="5 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="6 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="7 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="8 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="9 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="First Value" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Java代码
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
private static BottomSheetBehavior bottomSheetBehavior;
private static View bottomSheetInternal;
private static MyBottomSheetDialogFragment INSTANCE;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
CoordinatorLayout coordinatorLayout = (CoordinatorLayout)d.findViewById(R.id.locUXCoordinatorLayout);
bottomSheetInternal = d.findViewById(R.id.locUXView);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
bottomSheetInternal.requestLayout();
coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight();
Toast.makeText(getActivity(), "Height is" + bottomSheetInternal.getHeight() + " " + coordinatorLayout.getLayoutParams().height, Toast.LENGTH_LONG).show();
}
});
INSTANCE = this;
return inflater.inflate(R.layout.bottom_sheet_dialog_main, container, false);
}
}
通过更深入的 UI 检查,我们发现还有另一个 CoordinatorLayout
包装了我们的协调器布局。父 CoordinatorLayout
有一个 FrameLayout
和一个 BottomSheetBehavior
,id 为 design_bottom_sheet
。由于 id 为 design_bottom_sheet
的 FrameLayout
的 match_parent
高度,我们上面代码中设置的 peek 高度受到限制
通过设置 ID 为 design_bottom_sheet 的 FrameLayout
的 peek 高度,此问题已解决
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
coordinatorLayout = (CoordinatorLayout) d.findViewById(R.id.locUXCoordinatorLayout);
bottomSheetInternal = d.findViewById(R.id.locUXView);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
bottomSheetBehavior.setHidable(false);
BottomSheetBehavior.from((View)coordinatorLayout.getParent()).setPeekHeight(bottomSheetInternal.getHeight());
bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
coordinatorLayout.getParent().requestLayout();
}
});
我找到了另一个解决方案。也许对未来的读者有用。
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
final View root = View.inflate(getContext(), R.layout.fragment_bottom_sheet_choose_time, null);
dialog.setContentView(root);
initView(root);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) root.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
mBottomSheetBehavior = (BottomSheetBehavior) behavior;
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = root.getMeasuredHeight();
mBottomSheetBehavior.setPeekHeight(height);
}
});
}
}
正如@Anthonyeef 所提到的,这里的ViewTreeObserver
的目的是在真正测量视图并移除GlobalOnLayoutListener
以获得更好的性能后获得准确的测量高度。
但是,在生产中使用之前,请在不同的设备和屏幕上测试此解决方案,因为如果底部的内容 sheet 高于屏幕,它会产生一些奇怪的滑动行为。
在 onCreateView 中使用此代码。
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
coordinatorLayout.getParent().requestLayout();
}
});
感谢@athysirus 的简洁方法。这是我最终得到的版本,以防有人想要一个可用的 kotlin 示例。
需要注意的重要一点是,一旦完成,您还应该删除全局布局侦听器。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val bottomSheet = (dialog as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
BottomSheetBehavior.from<View>(bottomSheet).apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = 0
}
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
受 Nandish A 的 post 启发的 Kotlin 解决方案更详细。首先布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/container_root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!-- content of the bottom sheet -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
然后将其放入您的 BottomSheetDialogFragment
:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// ...
dialog.setOnShowListener {
val root = dialog.find<CoordinatorLayout>(R.id.container_root)
val bottomSheetInternal = root.find<ConstraintLayout>(R.id.bottom_sheet)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal)
bottomSheetBehavior.isHideable = false
BottomSheetBehavior.from(root.parent as View).peekHeight = root.height
bottomSheetBehavior.peekHeight = root.height
root.parent.requestLayout()
}
// ...
}
这就是我在 BottomSheetDialogFragment
中将 peek_height
和 layout_height
设置为底部 sheet 视图的方式
dialog?.setOnShowListener {
val dialog = dialog as BottomSheetDialog
val bottomSheet = dialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val coordinatorLayout = bottomSheet?.parent as? CoordinatorLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheet?.viewTreeObserver?.addOnGlobalLayoutListener {
bottomSheet.viewTreeObserver.removeOnGlobalLayoutListener {}
bottomSheetBehavior.peekHeight = getPopupHeight(.5f)
val params = bottomSheet.layoutParams
params.height = getPopupHeight(1f)
bottomSheet.layoutParams = params
coordinatorLayout?.parent?.requestLayout()
}
}
此方法获取屏幕高度的百分比
private fun getPopupHeight(percent: Float): Int {
val displayMetrics = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
return (displayMetrics.heightPixels * percent).toInt()
}
Kotlin 安全的实现方式是:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog.setOnShowListener {
val dialog = it as BottomSheetDialog
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
bottomSheet?.let { sheet ->
dialog.behavior.peekHeight = sheet.height
sheet.parent.parent.requestLayout()
}
}
}
注意: 无需将您的布局包装在协调器布局中。
很有魅力。
这对我来说是个窍门!我的 class 扩展了 BottomSheetDialogFragment
@Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
}
View view = getView();
view.post(() -> {
View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
View parent = (View) view.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
((View) bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT);
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.screen_delivery_type, container, false);
getDialog().setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.bottom_sheet);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
bottomSheetBehavior.setFitToContents(true);
bottomSheetBehavior.setExpandedOffset(0);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
coordinatorLayout.getParent().requestLayout();
}
});
}
此代码是答案的组合。因为其中一些对我不起作用。
在XML ids.xml:
<item name="sheet_parent_container" type="id" />
在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/sheet_parent_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
**Dialog content here!**
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在BottomSheetDialogFragment.kt中:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
setPeekHeight(view)
}
/**
* Function for disable half height display after screen rotation.
*/
private fun setPeekHeight(view: View) {
val parentContainer = view.findViewById<CoordinatorLayout>(R.id.sheet_parent_container)
dialog?.setOnShowListener {
val dialogParent = parentContainer.parent as View
BottomSheetBehavior.from(dialogParent).peekHeight = parentContainer.height
dialogParent.requestLayout()
}
}
我有以下测试底部 sheet 实现。
当我将 peekHeight 设置为小于 500 的值时,它起作用了。在某个值之后,任何 peek 高度的增加都不会改变底部 sheet 的扩展方式。它只是保留在那里只能手动拖动。我们如何以编程方式设置 peekHeight 以确保底部 sheet 自动扩展到 peek 高度。
bottom_sheet_dialog_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/locUXCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/locUXView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="1 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="2 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="3 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="4 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="5 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="6 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="7 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="8 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="9 Value" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="First Value" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Java代码
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
private static BottomSheetBehavior bottomSheetBehavior;
private static View bottomSheetInternal;
private static MyBottomSheetDialogFragment INSTANCE;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
CoordinatorLayout coordinatorLayout = (CoordinatorLayout)d.findViewById(R.id.locUXCoordinatorLayout);
bottomSheetInternal = d.findViewById(R.id.locUXView);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
bottomSheetInternal.requestLayout();
coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight();
Toast.makeText(getActivity(), "Height is" + bottomSheetInternal.getHeight() + " " + coordinatorLayout.getLayoutParams().height, Toast.LENGTH_LONG).show();
}
});
INSTANCE = this;
return inflater.inflate(R.layout.bottom_sheet_dialog_main, container, false);
}
}
通过更深入的 UI 检查,我们发现还有另一个 CoordinatorLayout
包装了我们的协调器布局。父 CoordinatorLayout
有一个 FrameLayout
和一个 BottomSheetBehavior
,id 为 design_bottom_sheet
。由于 id 为 design_bottom_sheet
FrameLayout
的 match_parent
高度,我们上面代码中设置的 peek 高度受到限制
通过设置 ID 为 design_bottom_sheet 的 FrameLayout
的 peek 高度,此问题已解决
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
coordinatorLayout = (CoordinatorLayout) d.findViewById(R.id.locUXCoordinatorLayout);
bottomSheetInternal = d.findViewById(R.id.locUXView);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
bottomSheetBehavior.setHidable(false);
BottomSheetBehavior.from((View)coordinatorLayout.getParent()).setPeekHeight(bottomSheetInternal.getHeight());
bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
coordinatorLayout.getParent().requestLayout();
}
});
我找到了另一个解决方案。也许对未来的读者有用。
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
final View root = View.inflate(getContext(), R.layout.fragment_bottom_sheet_choose_time, null);
dialog.setContentView(root);
initView(root);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) root.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
mBottomSheetBehavior = (BottomSheetBehavior) behavior;
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = root.getMeasuredHeight();
mBottomSheetBehavior.setPeekHeight(height);
}
});
}
}
正如@Anthonyeef 所提到的,这里的ViewTreeObserver
的目的是在真正测量视图并移除GlobalOnLayoutListener
以获得更好的性能后获得准确的测量高度。
但是,在生产中使用之前,请在不同的设备和屏幕上测试此解决方案,因为如果底部的内容 sheet 高于屏幕,它会产生一些奇怪的滑动行为。
在 onCreateView 中使用此代码。
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
coordinatorLayout.getParent().requestLayout();
}
});
感谢@athysirus 的简洁方法。这是我最终得到的版本,以防有人想要一个可用的 kotlin 示例。
需要注意的重要一点是,一旦完成,您还应该删除全局布局侦听器。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val bottomSheet = (dialog as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
BottomSheetBehavior.from<View>(bottomSheet).apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = 0
}
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
受 Nandish A 的 post 启发的 Kotlin 解决方案更详细。首先布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/container_root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<!-- content of the bottom sheet -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
然后将其放入您的 BottomSheetDialogFragment
:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// ...
dialog.setOnShowListener {
val root = dialog.find<CoordinatorLayout>(R.id.container_root)
val bottomSheetInternal = root.find<ConstraintLayout>(R.id.bottom_sheet)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal)
bottomSheetBehavior.isHideable = false
BottomSheetBehavior.from(root.parent as View).peekHeight = root.height
bottomSheetBehavior.peekHeight = root.height
root.parent.requestLayout()
}
// ...
}
这就是我在 BottomSheetDialogFragment
peek_height
和 layout_height
设置为底部 sheet 视图的方式
dialog?.setOnShowListener {
val dialog = dialog as BottomSheetDialog
val bottomSheet = dialog.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val coordinatorLayout = bottomSheet?.parent as? CoordinatorLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheet?.viewTreeObserver?.addOnGlobalLayoutListener {
bottomSheet.viewTreeObserver.removeOnGlobalLayoutListener {}
bottomSheetBehavior.peekHeight = getPopupHeight(.5f)
val params = bottomSheet.layoutParams
params.height = getPopupHeight(1f)
bottomSheet.layoutParams = params
coordinatorLayout?.parent?.requestLayout()
}
}
此方法获取屏幕高度的百分比
private fun getPopupHeight(percent: Float): Int {
val displayMetrics = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
return (displayMetrics.heightPixels * percent).toInt()
}
Kotlin 安全的实现方式是:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog.setOnShowListener {
val dialog = it as BottomSheetDialog
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
bottomSheet?.let { sheet ->
dialog.behavior.peekHeight = sheet.height
sheet.parent.parent.requestLayout()
}
}
}
注意: 无需将您的布局包装在协调器布局中。
很有魅力。
这对我来说是个窍门!我的 class 扩展了 BottomSheetDialogFragment
@Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
}
View view = getView();
view.post(() -> {
View bottomSheet = dialog.findViewById(R.id.bottom_sheet);
View parent = (View) view.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
((View) bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT);
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.screen_delivery_type, container, false);
getDialog().setOnShowListener(new DialogInterface.OnShowListener()
{
@Override
public void onShow(DialogInterface dialog)
{
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.bottom_sheet);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
bottomSheetBehavior.setFitToContents(true);
bottomSheetBehavior.setExpandedOffset(0);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
coordinatorLayout.getParent().requestLayout();
}
});
}
此代码是答案的组合。因为其中一些对我不起作用。
在XML ids.xml:
<item name="sheet_parent_container" type="id" />
在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/sheet_parent_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
**Dialog content here!**
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在BottomSheetDialogFragment.kt中:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
setPeekHeight(view)
}
/**
* Function for disable half height display after screen rotation.
*/
private fun setPeekHeight(view: View) {
val parentContainer = view.findViewById<CoordinatorLayout>(R.id.sheet_parent_container)
dialog?.setOnShowListener {
val dialogParent = parentContainer.parent as View
BottomSheetBehavior.from(dialogParent).peekHeight = parentContainer.height
dialogParent.requestLayout()
}
}