BottomSheetDialogFragment问题

1、CoordinatorLayout
BottomSheetDialog的布局根view需要使用CoordinatorLayout,并且子view需要设置behavior

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/login_backgroud">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/login_backgroud"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            android:id="@+id/design_bottom_sheet">

              .....
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

然后取behavior

mBehavior = BottomSheetBehavior.from(design_bottom_sheet)

2、设置peek高度(任意高度或者占满屏幕)

override fun onStart() {
        super.onStart()
        val dialog = dialog

        if (dialog != null) {
            val bottomSheet:View = dialog.findViewById(R.id.design_bottom_sheet)
            bottomSheet.layoutParams.height = Consts.screenHeight- Consts.statusBarHeight
        }
        val view = view
        view!!.post {
            val parent = view.parent as View
            val params = parent.layoutParams as CoordinatorLayout.LayoutParams
            val behavior = params.behavior
            mBehavior = behavior as BottomSheetBehavior<*>
            //mBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback)
            //设置高度
            mBehavior.peekHeight = Consts.screenHeight- Consts.statusBarHeight
            parent.setBackgroundColor(Color.TRANSPARENT)
        }
    }

3、状态栏变黑的问题
解决办法:通过设置全屏theme来把状态栏隐藏掉,去掉了难看的黑条

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(R.style.SheetStyle,R.style.TransparentBottonSheetStyle)
    }


 <style name="TransparentBottonSheetStyle" parent="Theme.Design.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/SheetStyle</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    <style name="SheetStyle" parent="android:Widget">
        <item name="android:background">@color/white</item>
        <item name="behavior_peekHeight">auto</item>
        <item name="behavior_hideable">true</item>
        <item name="behavior_skipCollapsed">false</item>
    </style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容