【问题标题】:Android BottomSheetDialogFragment has color behind rounded cornersAndroid BottomSheetDialogFragment 在圆角后面有颜色
【发布时间】:2019-06-03 05:45:12
【问题描述】:

我正在使用BottomSheetDialogFragment,我正在对右上角/左上角进行圆角处理,它工作正常,但我注意到在圆角后面,它不透明而且很烦人。

在下面的截图中很明显:

如何使它们透明?

【问题讨论】:

  • 我遇到了同样的问题。我在我的应用程序中使用com.sothree.slidinguppanel:library:3.4.0 库。
  • 尝试在 onStart() 方法中添加 getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); BottomSheetDialogFragment 类。
  • @Amrnoid 没有区别。
  • 运气好能找到解决方案吗?
  • 你可以用不同的方式来做。检查这个answer

标签: android bottom-sheet


【解决方案1】:

创建如下自定义样式。

 <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 上其他几个答案的唯一帮助!
【解决方案2】:

您必须更改bottom sheet theme 才能实现顶部圆形布局

创建自定义可绘制背景_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>

然后使用drawable作为背景覆盖styles.xml上的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
【解决方案3】:

在您的 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));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多