Android BottomSheetDialogFragment 在圆角后面有颜色。

10
我在使用BottomSheetDialogFragment,并将顶部右/左侧进行了圆角处理,这个效果很好,但我发现在圆角后面并不透明,这非常令人烦恼。
如下面的屏幕截图所示:

enter image description here

我该如何使它们透明呢?

我正在遇到相同的问题。 我在我的应用程序中使用了“com.sothree.slidinguppanel:library:3.4.0”库。 - Ajay Mehta
1
尝试在BottomSheetDialogFragment类的onStart()方法中添加getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); - Amrdroid
1
@Amrnoid 没有任何区别。 - Vahid Amiri
找到解决方案了吗? - Morteza Rastgoo
@MortezaRastgoo 不是的,甚至有些谷歌应用程序也存在这个问题。 - Vahid Amiri
1
你可以用不同的方式来实现。查看这个答案 - Gabriele Mariotti
3个回答

28

创建自定义样式,方法如下:

 <style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/rounded_corner_top_only</item>
    </style>

然后在自定义片段中覆盖此方法。

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //bottom sheet round corners can be obtained but the while background appears to remove that we need to add this.
        setStyle(DialogFragment.STYLE_NO_FRAME,R.style.AppBottomSheetDialogTheme);
    }

这对我有效,希望对你也有用。


我特意登录来给你的答案点赞。这是 Stackoverflow 上几个答案中唯一有用的东西! - Shubham Rohila
非常感谢您的答案和简单易懂的解释! - AnirbanMukherjee

1

您需要更改底部弹出框主题以实现顶部圆角布局

创建自定义可绘制的background_bottom_sheet_dialog_fragment.xml背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <corners
       android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
    <padding android:top="0dp" />
    <solid android:color="@color/white" />
</shape>

然后在styles.xml上使用drawable作为背景覆盖bottomSheetDialogTheme:

<!--Bottom sheet-->
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item 
    name="android:background">@drawable/background_bottom_sheet_dialog_fragment
    </item>
</style>

<style name="BaseBottomSheetDialog" 
    parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="bottomSheetStyle">@style/BottomSheet</item>
</style>

<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog" />

这将更改您底部工作表的背景布局

注意:从您的底部工作表对话框视图布局中删除所有背景


不支持API<21。 如何实现向后兼容? - Morteza Rastgoo

0

在你的BottomSheetDialogFragment中重写此方法

@Override
public void setupDialog(Dialog dialog, int style) {
    View view = View.inflate(getContext(), R.layout.YOUR_LAYOUT, null);
    dialog.setContentView(view);
    ((View) view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接