AppCompatDialogFragment change background color

问题: How do I change the background color of AppCompatDialogFragment. My class is extends AppCompatDialogFragment and I don't know how to change the property background color...

问题:

How do I change the background color of AppCompatDialogFragment.

My class is extends AppCompatDialogFragment and I don't know how to change the property background color of all the dialog.

public class MyClassName extends AppCompatDialogFragment { ...}

回答1:

You can use the same method posted here about making the background transparent and change transparent to color.

Create onCreateView and inside add the following line: getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));

Change Color.YELLOW to the background color that you want.

Full example:

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

If you want color from colors resources use:

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

Were colorPrimary is color resources name.

  • 发表于 2019-03-23 23:59
  • 阅读 ( 280 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除