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

时间:2018-07-31 10:01:50

标签: android

如何删除底部对话框对话框中的白色背景?

我尝试了this中的答案,或者将布局xml中的背景设置为透明,但仍然得到了结果

enter image description here

这是我的代码

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

<?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>

5 个答案:

答案 0 :(得分:3)

您需要将透明背景设置为底部工作表视图本身。

以下是科特林的一个例子:

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)
    }
}

答案 1 :(得分:2)

您必须为底部工作表对话框片段覆盖“onCreateDialog”函数,就像在 kotlin 中这样:

library(gridExtra)
g1 <- grid.arrange(Pie_2014_graph, Pie_2015_graph , Pie_total_graph, nrow = 1)
g2 <- grid.arrange(Bar_total_plot, Bar_years_plot, nrow = 1)
g3 <- grid.arrange(ts_1, ts_2, nrow = 1)
grid.arrange(g1, g2, g3, ncol = 1)

答案 2 :(得分:1)

使用此主题更改对话框的背景颜色

<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>

您的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
}

答案 3 :(得分:0)

对我来说,它可以通过编程使背景透明:

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

答案 4 :(得分:0)

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