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>