Android 设置背景图像警报对话框

Android 设置背景图像警报对话框,android,android-alertdialog,Android,Android Alertdialog,我正在尝试创建一个自定义的警报对话框,其中包含按钮和射线组。我想将背景图像保存在此AlertDialog的可绘制文件中 AlertDialog.Builder alert = new AlertDialog.Builder(AutoGenerate.this); alert.setTitle("Select color"); 是否有类似于alert.setbackgrounddrawable(R.drawable.img)的可选选项?您可以像这样设置警告

我正在尝试创建一个自定义的
警报对话框
,其中包含
按钮
射线组
。我想将背景图像保存在此
AlertDialog
的可绘制文件中

AlertDialog.Builder alert = new AlertDialog.Builder(AutoGenerate.this);                    
alert.setTitle("Select color");

是否有类似于
alert.setbackgrounddrawable(R.drawable.img)
的可选选项?

您可以像这样设置警告对话框的图标

alert.setIcon(R.drawable.image);

带有自定义视图的自定义对话框

    Dialog d = new Dialog(AutoGenerate.this);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.alertdialog);// custom layour for dialog.
    Button thankyou = (Button) d.findViewById(R.id.thankyou);
    d.show();
alertdialog.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_layout_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
android:padding="10dp"
 >
<Button
    android:id="@+id/thankyou"
    android:layout_width="100dp"
    android:layout_height="60dp"
    android:layout_gravity="center_horizontal" 
    android:text="hello"

    android:padding="5dp">

          </Button>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout> 

您可以使用布局文件来实现这一点。检查下面的代码

LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show(); 
dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>


您还可以在xml中使用不同的视图。

请查看。

是的,有一个相对简单的变体可用于AlertDialog:

// your code above
AlertDialog.Builder alert = new AlertDialog.Builder(AutoGenerate.this);                    
alert.setTitle("Select color");
// first show the dialog, then access its window and there set the background
alert.show();
alert.getWindow().setBackgroundDrawable(R.drawable.img);

您可以使用自定义视图创建自定义对话框。可能重复:您想在自定义警报对话框中设置背景,是吗?图标是,已创建,但想创建背景图像,图标在顶部设置图像尝试使用自定义警报对话框它不是背景,它是图标。您好,谢谢您的回复,让classcast异常执行此操作,任何想法y??只需更改此行-->LayoutFlater充气器=(LayoutFlater)this.getSystemService(Context.LAYOUT\u充气器\u服务);您好,谢谢您的回复,我使用的是单击单选按钮的侦听器时弹出的此警报拨号,尝试在radiobutton.setonclicklistener中插入此代码,获取classcast异常,有什么想法吗?如果(selected_option.equals(“No”)| selected_option.equals(“No”)| selected_option.equals(“No”)){LayoutInflater充气器=getLayoutInflater();视图对话框layout=充气器.充气(R.layout.customdialog,(ViewGroup)getCurrentFocus());最终字符序列[]性别={“男性”,“女性”};对话框d=新对话框(自动生成.this);d.requestWindowFeature(窗口.功能\u编号\u标题);d.setContentView(R.layout.customdialog);//dialog.Button Thankyu=(Button)d.findviewbyd(R.id.Thankyu);d.show();}嗨,在布局内部,有一个文本视图,如果我使用findviewbyd访问对话框内部并尝试设置标题,得到空指针异常,有什么想法吗?让我们来看看
// your code above
AlertDialog.Builder alert = new AlertDialog.Builder(AutoGenerate.this);                    
alert.setTitle("Select color");
// first show the dialog, then access its window and there set the background
alert.show();
alert.getWindow().setBackgroundDrawable(R.drawable.img);