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

时间:2016-06-21 18:23:35

标签: android android-layout android-alertdialog

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

我有一个自定义的身体视图以及我的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();


}

编辑这是我用于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"/>

3 个答案:

答案 0 :(得分:7)

为什么他们之间应该有差距?正面和负面按钮从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);
        }
    });

答案 1 :(得分:1)

如果我们需要在按钮之间留一个间隙,为什么我们不能即兴发挥?您可以使用中性按钮代替使用否定按钮。这是因为正面和负面是不可分割的,但是中立和正面在最右边和最左边都很好。所以只需将负按钮更改为中性即可。请参阅以下代码:

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

答案 2 :(得分:1)

对应用中所有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>