如何自定义 alertdialog 以便按钮适合 alertdialog

how to customize alertdialog so that buttons fit in the 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 - 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();

以上是按照上面link的建议完成的代码。我只得到了这个:

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

谁能解决这个问题?

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

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;

我得到了和上面一样的结果

更改对话框的主题。

它将解决您的问题。

使用 xml 布局中的 Holo.Dialog 并通过预览进行检查。

如果您想要这样的按钮,则必须使用 weightSum

希望对您有所帮助。

您好,您可以使用自己的自定义布局制作自定义对话框 她是示例代码

 private void CustomAlert ()
{

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.CustomViewName );
        dialog.setTitle(R.string.Upload_File_now);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        final EditText description_editTextField = (EditText) dialog.findViewById(R.id.descriptionEditText);
        final EditText locationEditText = (EditText) dialog.findViewById(R.id.placeEditText);

        Button cancel_btn = (Button) dialog.findViewById(R.id.cancel_btn);
        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });

        Button send_btn = (Button) dialog.findViewById(R.id.send_btn);
        send_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String location = locationEditText.getText().toString().trim();
                String description = description_editTextField.getText().toString().trim();
                boolean isUrgent = ((CheckBox)dialog.findViewById(R.id.checkBoxUrgent)).isChecked();

                dialog.cancel();
            }
        });
        dialog.show();
    }

希望这对您的项目有所帮助

您好,您可以像这样使用 CustomDialog

在XML

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

<ImageView
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:contentDescription="@drawable/ic_launcher"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:layout_centerHorizontal="true"
    android:text="Dismiss" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button"
    android:layout_toRightOf="@+id/image"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

活动中

final Dialog dialog = new Dialog(MainActivity.this);

        //setting custom layout to dialog
        dialog.setContentView(R.layout.cusotm_dialog_layout);
        dialog.setTitle("Custom Dialog");

        //adding text dynamically
        TextView txt = (TextView) dialog.findViewById(R.id.textView);
        txt.setText("Put your dialog text here.");

        ImageView image = (ImageView)dialog.findViewById(R.id.image);
        image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

        //adding button click event
        Button dismissButton = (Button) dialog.findViewById(R.id.button);
        dismissButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();

试试你这样的提示对话框代码:

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