繁体   English   中英

为什么Android AlertDialog没有占据屏幕的全高

[英]Why Android AlertDialog is not taking full height of screen

我正在学习Android Studio并面临Alertbox的问题。 为什么警告框在背景中不是透明的全高? 我搜索过它,但所有技巧都失败了。 我是android studio的新手,所以你的一些解释对我有帮助。

这是我的代码。 最后的图片显示图中的问题。

// get prompts.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.add_group_popup, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        this);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);

final EditText userInput = (EditText) promptsView
        .findViewById(R.id.editTextDialogUserInput);

// set dialog message
alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // get user input and set it to result
                        // edit text
                     //   result.setText(userInput.getText());
                    }
                })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();

add_group_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter the Group Name : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>

</LinearLayout>

在此输入图像描述

我有同样的问题,最后我可以通过将AlertDialog替换为Dialog来解决这个问题。

所以解决方案是这样的:

Dialog dialog = 
    new Dialog(
        this,
        android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen
    );
dialog.setContentView(R.layout.yourCustomLayoutDialog);

希望这有帮助..

您可以显式设置对话框的高度和宽度以匹配onStart中的屏幕

public void onStart() {
  super.onStart();
  DisplayMetrics metrics = getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;
  getDialog().getWindow().setLayout(width, height);
}

当我在活动中编写类似的方法时我偶然看到并从片段中使用它,它是全屏显示的。

public AlertDialog showAlert(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme);
        builder.setMessage(message);
        builder.setPositiveButton("OK", null);
        return builder.show();
}

使用Window manager以编程方式设置对话框大小。

 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(rate.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    alertdialog.show();
    alertdialog.getWindow().setAttributes(lp);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM