【问题标题】:AppCompatDialogFragment change background colorAppCompatDialogFragment 更改背景颜色
【发布时间】:2019-03-21 17:33:23
【问题描述】:

如何更改AppCompatDialogFragment的背景颜色。

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

public class MyClassName extends AppCompatDialogFragment { ...}

【问题讨论】:

    标签: android appcompatdialogfragment


    【解决方案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 是颜色资源名称。

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 1970-01-01
      • 2012-04-21
      • 2017-08-11
      • 2015-10-24
      • 2015-06-23
      相关资源
      最近更新 更多