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

Jak*_*emm 5 android android-layout android-alertdialog

在我的AlertDialog中,我的正按钮和负按钮是"附加的".我很确定他们之间应该有差距.有人能告诉我为什么会这样吗?我很乐意提供任何代码. 这是我的AlertDialog的样子.

我有一个自定义的身体视图以及我的AlertDialog的标题(我不会发布该XML代码,因为我认为这不是必要的,但请告诉我.)在MainActivity中,我夸大我的自定义标题和正文查看并覆盖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();


}
Run Code Online (Sandbox Code Playgroud)

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

<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"/>
Run Code Online (Sandbox Code Playgroud)

use*_*142 9

为什么他们之间应该有差距?正面和负面按钮从AlertDialog类中获得它们的布局尺寸,而不是从我记忆中,按钮之间没有任何余量.

为了添加边距,您可以自己创建按钮而不使用正面和负面按钮,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);
        }
    });
Run Code Online (Sandbox Code Playgroud)


小智 6

如果我们的按钮之间需要间隙,为什么我们不即兴创作呢?可以使用中性按钮代替负号按钮。这是因为正数和负数是不可分割的,但是中性和正数在最右边的一个很好,而在最左边的一个很好。因此,只需将否定按钮更改为中性即可。参见下面的代码:

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
                        }
                    });
Run Code Online (Sandbox Code Playgroud)

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

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();
    }
});
Run Code Online (Sandbox Code Playgroud)