Android AlertDialog按钮定位

标签 android android-layout android-alertdialog

我需要创建一个AlertDialog,它垂直分为两个区域:

  1. 其中一个区域(右侧)的底部应有对话框按钮(确定、取消和设置),而这些按钮的顶部应有另一种布局。
  2. 此对话框的左侧应该有另一个布局,例如图像。

问题是,是否有可能有类似的东西,或者所有警报对话框都必须将所有按钮放在底部?我什至不知道从哪里开始。我尝试查看 AlertDialog 类,但可以看到任何合适的内容。

最佳答案

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>

关于Android AlertDialog按钮定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38570266/

相关文章:

android - 在 ConstraintLayout 中使用 minHeight 查看

java - 安卓8(API 26): "colorControlHighlight" color is coming over contents

java - 更改 AlertDialog 中的字体

java - 按下警报对话框按钮始终返回 0 值

android - 如何在 Google TV 上跟踪鼠标移动

android - 在 webview 中更改为后置摄像头 (Android)

android - 平板电脑和手机的多种布局

Android:在嵌套布局中动态添加 View

Android SQLite 删除表实际上并没有删除行

Android Alert Dialog - 如何在按下 OK 按钮后隐藏它