【发布时间】:2019-03-21 17:33:23
【问题描述】:
如何更改AppCompatDialogFragment的背景颜色。
我的类是extends AppCompatDialogFragment,我不知道如何更改所有对话框的属性背景颜色。
public class MyClassName extends AppCompatDialogFragment { ...}
【问题讨论】:
标签: android appcompatdialogfragment
如何更改AppCompatDialogFragment的背景颜色。
我的类是extends AppCompatDialogFragment,我不知道如何更改所有对话框的属性背景颜色。
public class MyClassName extends AppCompatDialogFragment { ...}
【问题讨论】:
标签: android appcompatdialogfragment
您可以使用与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 是颜色资源名称。
【讨论】: