簡體   English   中英

為什么我的兩個 AlertDialog 按鈕之間沒有間隙?

[英]Why is there no gap between my two AlertDialog buttons?

在我的 AlertDialog 中,我的 Positive Button 和 Negative Button 是“附加的”。 我很確定他們之間應該有差距。 有人能告訴我為什么會這樣嗎? 我很樂意提供任何代碼。 這是我的 AlertDialog 的樣子。

我有一個 Body 的自定義視圖以及我的 AlertDialog 的標題(我不會發布那個 XML 代碼,因為我認為沒有必要,但請告訴我。)在 MainActivity 中,我增加了我的自定義標題和正文視圖,並覆蓋 setPositive() 和 setNegative(),然后我使用 onShow() 自定義按鈕的顏色。

對不起,復雜的代碼,但幫助將不勝感激:)。 這是我的主要活動:

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"/>

為什么他們之間會有差距? 正面和負面按鈕從AlertDialog類中獲取其布局尺寸,據我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);
        }
    });

如果我們需要您的按鈕之間的間隙,我們為什么不即興發揮? 您可以使用中性按鈕而不是使用否定按鈕。 這是因為正面和負面是不可分割的,但是中性和正面效果很好,一個在最右側,一個在最左側。 所以只需將否定按鈕更改為中性即可。 見下面的代碼:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM