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

时间:2015-10-20 08:28:52

标签: 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.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);
    }
}

这是它在设备上的外观: ttt

我想让它更广泛 - 我该怎么做?

3 个答案:

答案 0 :(得分:1)

您可以创建服装布局并在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"> 

答案 1 :(得分:1)

只需使用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);

答案 2 :(得分:1)

在显示对话框后使用getWindow()。setLayout(width,height)。

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