为什么我的两个 AlertDialog 按钮之间没有间隙?

Why is there no gap between my two AlertDialog buttons?

在我的 AlertDialog 中,肯定按钮和否定按钮是 "attached." 我很确定它们之间应该有一个间隙。有人能告诉我为什么会这样吗?我很乐意提供任何代码。 Here is what my AlertDialog looks like.

我有一个针对 Body 的自定义视图以及我的 AlertDialog 的标题(我不会 post 那 XML 代码,因为我认为没有必要,但请告诉我。)在 MainActivity 中,我膨胀了我的自定义标题和 body 视图,并覆盖了 setPositive() 和 setNegative(),然后我使用 onShow() 自定义了我的按钮的颜色。

对于复杂的代码,我们深表歉意,但我们将不胜感激 :)。这是我的 MainActivity:

public void openPrompt(View view){
    //builds and opens custom view with prompt.XML
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    EditText input = (EditText)promptView.findViewById(R.id.userInput);

    builder.setCancelable(true).setView(R.layout.customdialoglayout)
    .setNegativeButton("One", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show();
        }
    })
    .setPositiveButton("Two", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show();
        }
    });


    //set title with custom XML layout view
    LayoutInflater inflater = getLayoutInflater();
    View titleView = inflater.inflate(R.layout.cutomtitlebar,null);
    builder.setCustomTitle(titleView);

    AlertDialog ad = builder.create();

    //change colors of background and buttons
    ad.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            Context context = MainActivity.this;
            Window view = ((AlertDialog)dialog).getWindow();

            view.setBackgroundDrawableResource(R.color.colorPrompt);
           Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
            negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
            negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));

            Button posButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            posButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
            posButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));
        }
    });


    ad.show();


}

编辑 这是我的 XML 我用于 setView():

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="CUSTOM TEXT"
    android:id="@+id/textView3"
    android:layout_gravity="center"/>

他们之间为什么要有差距?正负按钮的布局尺寸来自 AlertDialog class,据我所知,按钮之间没有任何边距。

为了添加边距,您可以制作自己的按钮而不使用 AlertDialog 中的正负按钮,或者您可以采用与设置样式类似的方式为按钮添加边距按钮。

ad.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            Context context = MainActivity.this;
            Window view = ((AlertDialog)dialog).getWindow();

            view.setBackgroundDrawableResource(R.color.colorPrompt);
           Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
            negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton));
            negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText));

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(20,0,0,0);
            negButton.setLayoutParams(params);
        }
    });

如果我们需要在您的按钮之间留出间隙,我们为什么不即兴发挥呢?您可以使用中性按钮而不是使用否定按钮。这是因为正面和负面是密不可分的,但是中性和正面在最右侧和最左侧都可以正常工作。所以只需将否定按钮更改为中立即可。请参阅下面的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(preview.this);
                    builder.setTitle("Title here");
                    builder.setMessage("message on dialog here");
                    builder.setCancelable(true);
                    builder.setNeutralButton("Name of button N", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                           //your code here
                        }
                    });
                    builder.setPositiveButton("Name of button P", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //your code here
                        }
                    });

这是一个非常适合我的示例,您可以根据您的代码对其进行如下编辑:

builder.setCancelable(true).setView(R.layout.customdialoglayout)
.setNeutralButton("One", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show();
    }
})
.setPositiveButton("Two", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show();
    }
});

另一种为应用程序中的所有 AlertDialog 执行此操作的方法是更改​​ styles.xml

将自定义主题添加到您的 AlertDialog

//Kotlin code
val alertDialog = AlertDialog.Builder(this, R.style.AlertDialogCustom))

//Java Code
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);

在styles.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
//whichever parent theme you want to use here 
//add these 2 to allow all APIs
<item name="buttonBarPositiveButtonStyle">@style/PositiveAlertButtonStyle</item>
<item name="android:buttonBarPositiveButtonStyle">@style/PositiveAlertButtonStyle</item>

//change the marginStart of the Positive button to put a gap between the buttons
//you can also change text color and background etc. here
<style name="PositiveAlertButtonStyle">
<item name="android:layout_marginStart">10dp</item>