有什么方法可以删除AlertDialog的背景吗?

时间:2018-06-27 09:22:31

标签: android colors background alertdialog

我正在尝试更改Android AlertDialog的背景颜色。

我见过的每篇文章都是关于改变AlertDialog的颜色,而不是改变背景的。

任何帮助将不胜感激。

enter image description here

3 个答案:

答案 0 :(得分:3)

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>

,然后将其应用到Builder构造函数中:

AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
    ...
    .create();

有关更多信息,请参见see this post

答案 1 :(得分:2)

您可以减少暗淡的金额

dialog.getWindow().setDimAmount(0.5f);

要获得背景色,您需要创建一个自定义的“警告对话框”。

答案 2 :(得分:1)

有一种方法可以满足您的要求,但这并不是一个好的解决方案

首先,像这样设置自定义对话框主题。

styles.xml

    <style name="CustomDialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

CustomDialog.java

应用主题并按如下所示构建您的custom_dialog视图。

 public HTCustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}

custom_dialog.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_solid_80">

    <RelativeLayout
        android:id="@+id/dialog_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/bg_popup"
        android:padding="16dp">

</RelativeLayout>

现在,CustomDialog视图是全屏视图,将根布局的背景设置为所需的任何颜色。

enter image description here

我从here得到了这个答案。