2013-03-20 268 views
0

我想創建一個定製的AlertDialogButtonsRadioGroup。我想在這個AlertDialog的抽屜中保存背景圖片。設置背景圖像alertdialog

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

有沒有像alert.setbackgrounddrawable(R.drawable.img)任何替代方案?

+0

您可以使用自定義視圖創建自定義對話框。 – yahya 2013-03-20 09:52:06

+0

可能的重複項:[one](http://stackoverflow.com/q/5473058/420015)[two](http://stackoverflow.com/questions/7281963/how-to-change-the-background-image-警告對話框)[三](http://stackoverflow.com/questions/10347545/android-change-custom-alertdialog-background) – adneal 2013-03-20 09:53:49

+0

你想在你的自定義警報對話框中設置背景是這樣嗎? – GrIsHu 2013-03-20 09:58:17

回答

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

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

你好,謝謝你的答覆,我使用這個警報dialod彈出一個單選按鈕的點擊監聽器,當試圖插入radiobutton.setonclicklistener內的代碼,獲取classcast異常,任何想法? – bharath 2013-03-20 10:09:22

+0

可以發佈代碼。 – Raghunandan 2013-03-20 10:10:47

+0

如果(selected_option.equals( 「否」)|| selected_option.equals( 「否」)|| selected_option.equals( 「無」)) \t \t \t \t \t { \t \t \t \t \t \t LayoutInflater充氣= getLayoutInflater (); \t \t \t \t \t \t查看dialoglayout = inflater.inflate(R.layout.customdialog,(ViewGroup)getCurrentFocus()); \t \t \t \t \t \t final CharSequence [] gender = {「Male」,「Female」}; \t \t \t \t \t \t \t \t \t \t \t對話框d =新對話框(自動生成。這個); \t \t \t \t \t d.requestWindowFeature(Window.FEATURE_NO_TITLE); \t \t \t \t \t d.setContentView(R.layout.customdialog); //定製對話框的佈局。 \t \t \t \t \t Button thankyou =(Button)d.findViewById(R.id.thankyou); \t \t \t \t \t d.show(); \t \t \t \t \t} – bharath 2013-03-20 10:16:29

0

您可以設置圖標像自定義視圖此

alert.setIcon(R.drawable.image); 
+0

icon yup,已創建,但想創建背景圖片,圖標在頂部設置圖片 – bharath 2013-03-20 09:56:48

+0

嘗試使用自定義提醒對話框 – Madhuri 2013-03-20 10:00:44

+0

這不是背景,它是圖標。 – 2016-01-04 10:49:57

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

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中使用不同勢的看法。

+0

嗨謝謝回覆,得到classcast異常做它,任何想法? – bharath 2013-03-20 10:02:50

+0

just cheange this line ---> LayoutInflater inflater =(LayoutInflater)this \t \t \t \t .getSystemService(Context.LAYOUT_INFLATER_SERVICE); – 2013-03-20 10:25:18

+0

檢查我編輯的答案.. – 2013-03-20 10:25:34