简体   繁体   English

删除底部工作表对话框片段中的白色背景

[英]Remove White Background in Bottom Sheet Dialog Fragment

How to remove white background in bottom sheet dialog fragment?如何删除底部工作表对话框片段中的白色背景?

I have tried the answer in this or set the background in the layout xml to transparent, but still get this result我试图在回答这个或设置在布局XML透明背景,但仍得到这样的结果

在此处输入图片说明

Here is my code这是我的代码

public class BottomSheetFragment extends BottomSheetDialogFragment {

private Record record;

private MainFragment fragment;

public static BottomSheetFragment getInstance() {
    return new BottomSheetFragment ();
}

public BottomSheetFragment setRecord(Record record) {
    this.record = record;
    return this;
}

public BottomSheetFragment setFragment(MainFragment fragment) {
    this.fragment = fragment;
    return this;
}

@TargetApi(Build.VERSION_CODES.O)
@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT);
    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);

    //Set content
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.layout_bottom_sheet, container);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
}

layout_bottom_sheet.xml layout_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:background="@drawable/bg_cardboard">

</FrameLayout>

You will need to set transparent background to bottom sheet view itself.您需要为底部工作表视图本身设置透明背景。

Here is an example is Kotlin:下面是 Kotlin 的一个例子:

class YourBottomSheetFragment : BaseBottomSheetDialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dialog.setOnShowListener { setupBottomSheet(it) }
        return dialog
    }

    private fun setupBottomSheet(dialogInterface: DialogInterface) {
        val bottomSheetDialog = dialogInterface as BottomSheetDialog
        val bottomSheet = bottomSheetDialog.findViewById<View>(
            com.google.android.material.R.id.design_bottom_sheet)
            ?: return
        bottomSheet.setBackgroundColor(Color.TRANSPARENT)
    }
}

Use this theme to change background color for your dialog使用此主题更改对话框的背景颜色

<style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">Your custom color here</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:gravity">center</item>
    </style>

and your onViewCreated should be like or only add setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);并且您的onViewCreated应该像或只添加setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);

@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT);
  setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);

    //Set content
}

you must be override "onCreateDialog" function for your bottom sheet dialog fragment, like this in kotlin:您必须为底部工作表对话框片段覆盖“onCreateDialog”函数,就像在 kotlin 中这样:

        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.setOnShowListener {
            //this line transparent your dialog background
            (view?.parent as ViewGroup).background = 
            ColorDrawable(Color.TRANSPARENT)
        }

        return dialog
       }

对我来说,它以编程方式使背景透明:

((View) viewBotaoSheet.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent))

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何去除 android 中底部工作表对话框片段创建的阴影? - How to remove the shadow created by bottom sheet dialog fragment in android? 从底部工作表对话框片段中获取值 - Get value from Bottom Sheet Dialog Fragment 如何在底部工作表对话框片段中设置文本按钮? - how to settext button in bottom sheet dialog fragment? 片段内部的底页对话框 - Bottom Sheet Dialog from inside a fragment OnBackPressedCallback 未在底部工作表对话框片段中调用 - OnBackPressedCallback not called in Bottom Sheet Dialog Fragment 如何在底部工作表对话框上设置矢量背景 - How to set vector background on a Bottom Sheet Dialog 如何始终在底部工作表对话框片段中将按钮与屏幕底部对齐 - How to always align button to screen bottom in a bottom sheet dialog fragment 在基本片段上检测底部工作表对话框片段关闭 - Detect Bottom sheet dialog fragment dismiss on base fragment 将数据从底部工作表对话框片段传递到片段 - Pass data from Bottom Sheet Dialog Fragment to Fragment Android:在片段中滑动操作以打开底部工作表对话框 - Android: swipe action in fragment to open bottom sheet dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM