2016-07-25 86 views
2

我有一個要求,以創建一個AlertDialog,它被垂直分割成兩個區域:的Android AlertDialog按鈕定位

  1. 其中一個區(右側)的應有對話框按鈕(確定,取消和設置)在這些按鈕的頂部和另一個佈局。
  2. 此對話框的左側應該有另一個佈局,比如圖像。

問題是,是否有可能有類似的東西或所有的alertDialogs都必須在底部有所有的按鈕?我甚至不知道從哪裏開始。我試過看看AlertDialog班,但可以看到任何合適的東西。

+1

看看本作開始:http://stackoverflow.com/questions/13341560/how-to-create-a-custom -dialog-box-in-android – Zaki

+0

也許[this SO question](http://stackoverflow.com/questions/18352324/how-can-can-i-add-custom-buttons-into-an-alertdialogs-layout)可以得到一些幫助 –

+0

謝謝@Zaki。看起來像我可以嘗試.. – etilia

回答

1

它看起來像你不需要AlertDialog這個,但是你可以用AlertDialog來做到這一點。創建視圖,然後使用AlertDialog.Builder不調用setPositiveButton()setNegativeButton()等。

AlertDialog alertDialog = new AlertDialog.Builder(context) 
     .setView(yourContentView) 
     .create(); 
+0

謝謝,我認爲,這是我需要的 – etilia

0

您可以創建自己的自定義對話框佈局 - 與此3個步驟:

  1. 創建自定義對話框佈局(XML文件)。
  2. 將佈局附加到對話框。
  3. 顯示對話框。

Look here

3

enter image description here

AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, 
      R.style.MyAlertDialogStyle); 
    builder.setTitle(mActivity.getResources().getString(R.string.app_name)); 
    builder.setCancelable(false); 
    builder.setMessage(str_message); 
    builder.setPositiveButton(str_btn1, 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        if (listener != null) { 
         listener.onClick(null); 
        } 
       } 
      }); 
    if (str_btn2 != null) { 
     builder.setNegativeButton(str_btn2, 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, 
             int i) { 
         if (listener2 != null) { 
          listener2.onClick(null); 
         } 
        } 
       }); 
    } 
    builder.show(); 

編輯:請參見上面的圖像。你會得到這樣的輸出。只需在styles.xml創建一個對話框風格

對話風格

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons --> 
    <item name="android:textStyle">bold</item> 
    <item name="colorAccent">@android:color/holo_blue_bright</item> 
    <!-- Used for the title and text --> 
    <item name="android:textColorPrimary">@android:color/black</item> 
    <!-- Used for the background --> 
    <item name="android:background">@android:color/white</item> 
</style> 
+1

謝謝,但我需要按鈕在右下方,他們不應該全部對話。我目前已經實現了這個版本,但它需要定製。 – etilia

+0

看看我編輯的答案@etilia – abissa

+0

嗨,當然,這可能會帶來按鈕的右側。但是,還需要在對話框的左半部分覆蓋整個區域的圖像。因此,我認爲對於這個特定的場景選擇答案更合適。儘管如此,我仍然堅持我對未來的關注。謝謝你,@abissa。 – etilia