BottomSheetDialogFragment 设置背景为透明无效果(解决方法)
如题
BottomSheetDialogFragment 设置背景为透明无效果(解决方法)
设置了style没有效果
<style name="TransparentBottomSheet" parent="Theme.Design.Light.BottomSheetDialog"><!-- 关键属性:窗口透明且禁用遮罩 --><item name="android:windowBackground">@android:color/transparent</item><item name="android:background">@android:color/transparent</item><item name="backgroundDimEnabled">false</item><!-- 确保全屏覆盖,避免留白 --><item name="android:windowIsFloating">false</item>
</style>
解决方法
在 onViewCreated方法中添加
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)// 移除默认灰色遮罩dialog?.window?.apply {setDimAmount(0f) // 透明遮罩setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))}// 关键:将底部弹窗容器设为透明val bottomSheet = dialog?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)bottomSheet?.setBackgroundColor(Color.TRANSPARENT)
}
完美解决!