Android 如何更改标准警报对话框的大小并使其更大?

Android 如何更改标准警报对话框的大小并使其更大?,android,android-alertdialog,Android,Android Alertdialog,我有标准的AlertDialog弹出我的游戏结果,我想让它更大。 这是我的对话框创建和设置代码: @Override protected Dialog onCreateDialog(int id) { adb = new AlertDialog.Builder(this); win_lose_view = (RelativeLayout) getLayoutInflater().inflate(R.layout.win_lose_screen, null); adb.se

我有标准的AlertDialog弹出我的游戏结果,我想让它更大。 这是我的对话框创建和设置代码:

@Override
protected Dialog onCreateDialog(int id) {
    adb = new AlertDialog.Builder(this);
    win_lose_view = (RelativeLayout) getLayoutInflater().inflate(R.layout.win_lose_screen, null);
    adb.setView(win_lose_view);
    upper_text = (TextView) win_lose_view.findViewById(R.id.upper_text);
    upper_text.setRotation(180);
    lower_text = (TextView) win_lose_view.findViewById(R.id.lower_text);
    upper_decide = (Button) win_lose_view.findViewById(R.id.upper_decide);
    upper_decide.setRotation(180);
    lower_decide = (Button) win_lose_view.findViewById(R.id.lower_decide);
    Dialog d = adb.create();
    d.setCanceledOnTouchOutside(false);
    return d ;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog){
    super.onPrepareDialog(id, dialog);
    upper_decide = (Button) win_lose_view.findViewById(R.id.upper_decide);
    upper_decide.setRotation(180);
    lower_decide = (Button) win_lose_view.findViewById(R.id.lower_decide);     
    };
    if(first_player_points > second_player_points){
        upper_text.setText("you win! score: " + first_player_points);
        lower_text.setText("you lose! score: "+ second_player_points);
    }
}
以下是它在设备上的外观:


我想让它更宽-我该怎么做?

您可以创建自己的服装布局,并使用onCreateDialog方法进行设置

 @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = View.inflate(getActivity(),      R.layout.thalicancle_dialog, null);

        final Dialog dialog = new Dialog(getActivity(), 0);

        dialog.setContentView(view);
}

自定义布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/popupbg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"> 


您可以创建服装布局并使用onCreateDialog方法进行设置

 @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = View.inflate(getActivity(),      R.layout.thalicancle_dialog, null);

        final Dialog dialog = new Dialog(getActivity(), 0);

        dialog.setContentView(view);
}

自定义布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/popupbg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"> 


只需使用layoutParams并更改属性即可

    AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Test Dialog")
        .setMessage("This should expand to the full width")
        .show();
        //Grab the window of the dialog, and change the width
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = dialog.getWindow();
         lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
         lp.width = WindowManager.LayoutParams.MATCH_PARENT;
         lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
         window.setAttributes(lp);

只需使用layoutParams并更改属性

    AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Test Dialog")
        .setMessage("This should expand to the full width")
        .show();
        //Grab the window of the dialog, and change the width
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = dialog.getWindow();
         lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
         lp.width = WindowManager.LayoutParams.MATCH_PARENT;
         lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
         window.setAttributes(lp);
在显示对话框后使用getWindow().setLayout(宽度、高度)

alertDialogBuilder
        .setMessage("Click yes to exit!")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                MainActivity.this.finish();
            }
          })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        }).show().getWindow().setLayout(600,500);
在显示对话框后使用getWindow().setLayout(宽度、高度)

alertDialogBuilder
        .setMessage("Click yes to exit!")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                MainActivity.this.finish();
            }
          })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        }).show().getWindow().setLayout(600,500);

创建具有相同对话框行为的自定义视图?可能是在中心和黑暗背景下的相对布局?你的意思是,不使用任何标准警报对话框?就在游戏结束时加载吗?没错。因为你问的对谷歌来说不是一个好的做法。观察到对话框的最大大小。在你的情况下,这是例外。所以我认为您应该为此创建一个exeptionnel行为。创建一个与Dialog行为相同的自定义视图?可能是在中心和黑暗背景下的相对布局?你的意思是,不使用任何标准警报对话框?就在游戏结束时加载吗?没错。因为你问的对谷歌来说不是一个好的做法。观察到对话框的最大大小。在你的情况下,这是例外。所以我认为你应该创造一个例外的行为。