2

我正在尝试AlertDialog使用Buttons和创建自定义RadioGroup。我想为此在我的可绘制对象中保存一个背景图像AlertDialog

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

有没有其他选择alert.setbackgrounddrawable(R.drawable.img)

4

5 回答 5

2

你可以使用布局文件来做到这一点。检查下面的代码

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

对话框布局.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 中使用不同的视图。

于 2013-03-20T09:56:30.533 回答
2

是的,有一个相对简单的变体可以与 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);
于 2019-06-26T15:07:10.470 回答
1

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

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

警报对话框.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> 

编辑 :

    rb= (RadioButton) findViewById(R.id.radioButton1);
    rb.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showAlertDialog();
        }

    });       

     public void showAlertDialog() {

        final Dialog d = new Dialog(MainActivity.this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d.setContentView(R.layout.alertdialog);
        // Thank you Button Listener.
        final TextView tv = (TextView) d.findViewById(R.id.textView1);
        final Button thankyou = (Button) d.findViewById(R.id.thankyou);
        thankyou.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                      tv.setText("Button Clicked");

                return true;
            }

        });

        d.show();
    }       
于 2013-03-20T09:56:08.143 回答
0

您可以像这样将图标设置为警报对话框

alert.setIcon(R.drawable.image);
于 2013-03-20T09:50:33.587 回答
0

查看在 android 中创建自定义警报对话框

于 2013-03-20T10:09:30.427 回答