gpt4 book ai didi

android - AppCompatDialogFragment 更改背景颜色

转载 作者:行者123 更新时间:2023-11-29 23:16:22 27 4
gpt4 key购买 nike

如何更改 AppCompatDialogFragment 的背景颜色。

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

public class MyClassName extends 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 是颜色资源名称。

关于android - AppCompatDialogFragment 更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55286170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com