AppCompatDialogFragment更改背景颜色

时间:2019-03-21 17:33:24

标签: android appcompatdialogfragment

如何更改AppCompatDialogFragment的背景颜色。

我的课程是扩展AppCompatDialogFragment,我不知道如何更改所有对话框的属性背景颜色。

public class MyClassName extends AppCompatDialogFragment { ...}

1 个答案:

答案 0 :(得分:1)

您可以使用发布在here上的相同方法,使背景透明并更改颜色。

创建onCreateView并在其中添加以下行:getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));

Color.YELLOW更改为所需的背景颜色。

完整示例:

public class ClassName extends AppCompatDialogFragment {
    ...
    ...
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

如果要从颜色资源中使用颜色,请使用:

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getContext().getColor(R.color.colorPrimary)));

colorPrimary是颜色资源名称。

相关问题