2016-07-25 87 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