【问题标题】:set background image alertdialog设置背景图像警报对话框
【发布时间】:2013-03-09 08:14:06
【问题描述】:

我正在尝试使用ButtonsRadioGroup 创建一个自定义的AlertDialog。我想为这个AlertDialog 在我的drawable 中保存一个背景图像。

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

有没有像alert.setbackgrounddrawable(R.drawable.img)这样的替代选项?

【问题讨论】:

  • 您可以使用自定义视图创建自定义对话框。
  • 可能重复:onetwothree
  • 您想在自定义警报对话框中设置背景是这样吗?

标签: android android-alertdialog


【解决方案1】:

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

alert.setIcon(R.drawable.image);

【讨论】:

  • icon 是的,已创建,但想创建背景图像,icon 在顶部设置图像
  • 尝试使用自定义警报对话框
  • 不是背景,是图标。
【解决方案2】:

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

    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> 

编辑:

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

【讨论】:

  • 您好,感谢您的回复,我在单选按钮的单击侦听器上使用此警报 dialod 弹出,当尝试在 radiobutton.setonclicklistener 中插入此代码时,得到类广播异常,知道吗?
  • if(selected_option.equals("No")|| selected_option.equals("NO")|| selected_option.equals("no")) { LayoutInflater inflater = getLayoutInflater();查看 dialoglayout = inflater.inflate(R.layout.customdialog, (ViewGroup) getCurrentFocus());最终的 CharSequence[] 性别 = {"男","女"};对话框 d = new Dialog(AutoGenerate.this); d.requestWindowFeature(Window.FEATURE_NO_TITLE); d.setContentView(R.layout.customdialog);// 自定义对话框布局。按钮thankyou = (Button) d.findViewById(R.id.thankyou); d.show(); }
  • 嗨,在布局内部,有一个文本视图,如果我使用 findviewbyid 访问对话框内部并尝试设置标题,得到空指针异常,你知道吗?
【解决方案3】:

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

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 中使用不同的视图。

【讨论】:

  • 您好,感谢您的回复,得到了 classcast 异常,您知道吗??
  • 改行 ---> LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
【解决方案4】:

【讨论】:

    【解决方案5】:

    是的,有一个相对简单的变体可以与 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);
    

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2012-01-30
      • 2011-03-08
      • 2015-06-07
      相关资源
      最近更新 更多