如何使 BottomSheetDialog 全屏显示?

Posted

技术标签:

【中文标题】如何使 BottomSheetDialog 全屏显示?【英文标题】:How to make a BottomSheetDialog full screen? 【发布时间】:2021-12-20 02:12:36 【问题描述】:

我有一个 BottomSheetDialog 类,当我们单击按钮时会显示,我需要让它全屏而不是页面上的一半。

public class BottomSheetDialogBuyPlan extends BottomSheetDialog 
       public BottomSheetDialogBuyPlan(@NonNull Context context) 
    super(context);


    BottomSheetBehavior<FrameLayout> behavior = getBehavior();
    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    View bottomSheet = getLayoutInflater().inflate(R.layout.layout, null);
    setContentView(bottomSheet);
    show();
  

    @Override
    public void setOnShowListener(@Nullable OnShowListener listener) 
        super.setOnShowListener(listener);
    

这就是我在活动中的称呼

    BottomSheetDialogBuyPlan bottomSheetDialog = new 
   BottomSheetDialogBuyPlan(getContext());

如何让它全屏显示?

【问题讨论】:

***.com/questions/58065771/… 不工作这是bottomsheetdialogfragment不是bottomsheetdialog!! 【参考方案1】:

您在此处显示的代码非常有限,因此我也无法提供直接适用于您的案例的代码。至少我建议有BottomSheetBehavior#setState(BottomSheetBehavior.STATE_EXPANDED) 方法。

这是一个最低限度的示例代码:

主活动:

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        BottomSheetDialog dialog = new BottomSheetDialog(this);
        BottomSheetBehavior<FrameLayout> behavior = dialog.getBehavior();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        View bottomSheet = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
        dialog.setContentView(bottomSheet);
        dialog.show();
    

布局/bottom_sheet:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_>

    <TextView
        android:layout_
        android:layout_
        android:text="a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na"
        android:textSize="30sp" />

</ScrollView>

编辑:

您可以设法将 BottomSheet 的内容布局扩展到其最初需要的内容之外,但我怀疑您是否需要将 BottomSheet 用于此类用途...

MyBottomSheetDialog:

public class MyBottomSheetDialog extends BottomSheetDialog 
    public MyBottomSheetDialog(@NonNull Context context) 
        super(context);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager()
                .getDefaultDisplay()
                .getMetrics(displayMetrics);

        BottomSheetBehavior<FrameLayout> behavior = getBehavior();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        View bottomSheet = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
        bottomSheet.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, displayMetrics.heightPixels
        ));
        setContentView(bottomSheet);
    

布局/bottom_sheet:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_>

    <TextView
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:text="a\na"
        android:textSize="30sp" />
</FrameLayout>

【讨论】:

在所有情况下都不起作用我把这段代码放在 show() 函数之前并且不起作用 @Malo 你没有透露show() 方法。我什么都说不出来。 检查我的问题是否更新 @Malo show() 应在实例化后用于 Activity 中的 bottomSheetDialog(而不是 BottomSheetDialogBuyPlan)。 好的,只要文本有 30 个 'a' 字母,页面就会使用 textview 垂直扩展。否则它会显示页面的一半

以上是关于如何使 BottomSheetDialog 全屏显示?的主要内容,如果未能解决你的问题,请参考以下文章

如何在BottomSheetDialog android的顶部中心添加关闭按钮?

如何制作只有上角圆角的BottomSheetDialog? [复制]

如何设置自动布局使图像占据全屏?

单击按钮时如何使 VideoView 全屏显示?

如何使闪亮的应用程序自动全屏?

如何使内容div全屏