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

詹森

我正在使用以下代码来创建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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

如何在Android中创建自定义警报对话框?

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

具有自定义列表视图的自定义警报对话框

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Android自定义警报对话框Mediaplayer搜寻栏

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

在自定义对话框中删除警报对话框边框

来自分类Dev

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

来自分类Dev

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

来自分类Dev

如何在警报对话框中自定义颜色或突出显示PositiveButton和NegativeButton

来自分类Dev

如何设置或自定义React-admin警报对话框?

来自分类Dev

使用自定义按钮关闭具有自定义界面的警报对话框

来自分类Dev

捕获警报对话框按钮事件

来自分类Dev

警报对话框肯定按钮问题

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

    如何在Android中创建自定义警报对话框?

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

    具有自定义列表视图的自定义警报对话框

  8. 8

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

  9. 9

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

  10. 10

    Android自定义警报对话框Mediaplayer搜寻栏

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

    在自定义对话框中删除警报对话框边框

  23. 23

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

  24. 24

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

  25. 25

    如何在警报对话框中自定义颜色或突出显示PositiveButton和NegativeButton

  26. 26

    如何设置或自定义React-admin警报对话框?

  27. 27

    使用自定义按钮关闭具有自定义界面的警报对话框

  28. 28

    捕获警报对话框按钮事件

  29. 29

    警报对话框肯定按钮问题

热门标签

归档