自己使用BottomSheetDialogFragment時,想實現(xiàn)上方圓角蚁廓。
布局設(shè)置了圓角的背景后陌选,需要給dialog的北京設(shè)置為透明,才能有圓角的效果肴甸,網(wǎng)上其他的文章都是這么實現(xiàn)的:
dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
但是我使用時找不到R.id.design_bottom_sheet對應(yīng)的view
于是采用了修改style的方法.
style代碼
<style name="AppBottomSheet" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppBottomSheetStyle</item>
</style>
<style name="AppBottomSheetStyle" parent="Widget.MaterialComponents.BottomSheet.Modal">
<item name="backgroundTint">@color/colorTrans</item>
</style>
重寫B(tài)ottomSheetDialogFragment時寂殉,在onCreate中設(shè)置屬性即可實現(xiàn)圓角
/*
setStyle方法的注釋: Calling this after the fragment's Dialog iscreated will have no effect.
要在dialog創(chuàng)建出來之前設(shè)置好style
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL,R.style.AppBottomSheet)
}
實現(xiàn)效果: