android - 如何自定义警报对话框以使按钮适合警报对话框

标签 android android-alertdialog

我使用以下代码来创建警报对话框。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light));
    View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
    builder.setView(pickerView);
    builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    }).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "neutralize", Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog alert = builder.create();
    alert.setTitle("number picker");
    alert.show();

问题是当我使用此代码创建对话框时,这三个按钮放置不均匀,如下所示:

enter image description here

实际上我想要得到的是这样的:

enter image description here

这两个按钮同等放置

我知道这是alertdialog主题的问题。但我尝试了无数次,但我无法改变任何事情。

谁能告诉我如何处理主题才能获得像第二个这样的警报对话框?

picker_dialog的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="input"/>

</LinearLayout>

我遵循 Android - Make AlertDIalog buttons a uniform size 的建议。代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog));
    View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
    builder.setView(pickerView);
    builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("Verify", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
        }
    });

    final AlertDialog alert = builder.create();
    alert.setTitle("number picker");
    alert.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button posButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
            Button negButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
            Button neuButton = alert.getButton(DialogInterface.BUTTON_NEUTRAL);

            LinearLayout.LayoutParams posParams = (LinearLayout.LayoutParams) posButton.getLayoutParams();
            posParams.weight = 1;
            posParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            posParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

            LinearLayout.LayoutParams negParams = (LinearLayout.LayoutParams) negButton.getLayoutParams();
            negParams.weight = 1;
            negParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            posParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            LinearLayout.LayoutParams neuParams = (LinearLayout.LayoutParams) neuButton.getLayoutParams();
            neuParams.weight = 1;
            neuParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            neuParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;


            posButton.setLayoutParams(posParams);
            negButton.setLayoutParams(negParams);
            neuButton.setLayoutParams(neuParams);
        }
    });
    alert.show();

以上是按照上面链接的建议后的完整代码。我只得到这个:enter image description here

似乎正面按钮已推到右角并消失了。

有人能解决这个问题吗?

根据Kushan的建议,我从dialog onshow监听器中取出布局设置代码,完整代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Dialog));
    View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
    builder.setView(pickerView);
    builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("Positive", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog alert = builder.create();
    alert.setTitle("number picker");
    alert.show();

    LinearLayout.LayoutParams buttonParams;
    Button buttonPositive = alert.getButton(AlertDialog.BUTTON_POSITIVE);
    buttonParams = (LinearLayout.LayoutParams) buttonPositive.getLayoutParams();
    buttonParams.weight = 1;
    buttonParams.width = buttonParams.MATCH_PARENT;

    Button buttonNegative = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
    buttonParams = (LinearLayout.LayoutParams) buttonNegative.getLayoutParams();
    buttonParams.weight = 1;
    buttonParams.width = buttonParams.MATCH_PARENT;

    Button buttonNeutral = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
    buttonParams = (LinearLayout.LayoutParams) buttonNeutral.getLayoutParams();
    buttonParams.weight = 1;
    buttonParams.width = buttonParams.MATCH_PARENT;

我得到了与上面相同的结果

enter image description here

最佳答案

尝试如下所示的警报对话框代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light));
View pickerView = getLayoutInflater().inflate(R.layout.picker_dialog, null);
builder.setView(pickerView);
builder.setMessage("AlertDialog").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "got it!", Toast.LENGTH_SHORT).show();
    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
}).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "neutralize", Toast.LENGTH_SHORT).show();
    }
});

AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        Button negativeButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
        Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
        negativeButton.setLayoutParams(params);
        positiveButton.setLayoutParams(params);

        negativeButton.invalidate();
        positiveButton.invalidate();
    }
});
alert.setTitle("number picker");
alert.show();

关于android - 如何自定义警报对话框以使按钮适合警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555107/

相关文章:

android - 如何在 Mac 上的 Worklight Studio 中设置 Android Studio 路径

java - Android 中与蓝牙设备的第二次连接失败

android - 在 Android 中创建 AlertDialog

android - 在没有自定义 View 的情况下将 editText 放在 AlertDialog 中?

android - 带有 ArrayAdapter 的 AlertDialog 不显示项目列表

android - React-native:在 Android 中不会调用切换组件 onValueChange

android - 如何捕捉图像并将其保存在SD卡中

android - 如何制作 Android 平板电脑应用程序

android - 无法关闭带有自定义按钮的对话框

android - 从警报对话框中删除黑色背景 - Android