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

詹森

我正在使用以下代码段创建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();

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

在此处输入图片说明

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

在此处输入图片说明

这两个按钮均放在同一位置

我知道这是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上的建议-使AlertDIalog按钮的大小均匀代码如下:

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

以上是按照上面链接的建议编写的完整代码。而且我只有这个:在此处输入图片说明

似乎肯定按钮已推到右角并消失了。

有人可以解决这个问题吗?

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

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;

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

在此处输入图片说明

普兰尼

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何关闭“自定义警报”对话框

Android-如何在自定义警报对话框中检查按钮单击?

Android中的“自定义警报”对话框

在自定义警报对话框中获取单选按钮的值

自定义对话框警报

如何在自定义警报对话框中刷新内容/ UI

如何在Android中使用网格视图创建自定义警报对话框?

如何制作自定义警报对话框全屏

使 Multiautocompletetextview 在警报对话框中添加按钮上方可滚动,我想要这种类型的自定义警报对话框

制作圆形自定义警报对话框:着色区域不正确

Flutter-自定义警报对话框未显示

在服务android中显示自定义警报对话框

通过android自定义警报对话框传递额外数据

在自定义警报对话框中使用时 ExpandableListView 对象为 null

具有RecyclerView的自定义警报对话框

自定义警报对话框不会关闭

具有多个EditText的自定义警报对话框

两个活动的一个自定义警报对话框

在自定义警报对话框中从 Edittext 检索数据时显示空值?

自定义警报对话框搜索项目结果错误

自定义警报对话框的背景色

从Android中的ListView适配器启动自定义警报对话框

具有多个视图的自定义警报对话框

如何将自定义警报对话框中的项目添加到列表视图?

如何在自定义警报对话框中将信息从活动传递到编辑文本

实现自定义弹出窗口的正确方法?活动与对话框与警报对话框

Android警报对话框的自定义面板和按钮面板的高度为0

永远不会选中“警报”对话框中的“自定义单选按钮”

如何在Xamarin Android中基于设备的宽度和高度以编程方式创建自定义警报对话框?