Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android没有调整足够大的自定义对话框_Android_Dialog - Fatal编程技术网

Android没有调整足够大的自定义对话框

Android没有调整足够大的自定义对话框,android,dialog,Android,Dialog,我正在使用一个包含文本字段、图像和按钮的自定义对话框。文本可以包含HTML。有时,当文本足够长时,对话框的底部会被切掉。我怎样才能防止这种情况?我想让Android来决定对话框的大小,但它似乎没有这样做。在这种情况下,我需要自己调整对话框的大小吗 这是布局图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/alert_root_incorrect"

我正在使用一个包含文本字段、图像和按钮的自定义对话框。文本可以包含HTML。有时,当文本足够长时,对话框的底部会被切掉。我怎样才能防止这种情况?我想让Android来决定对话框的大小,但它似乎没有这样做。在这种情况下,我需要自己调整对话框的大小吗

这是布局图

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/alert_root_incorrect"
  style="@style/AlertDialogTheme"
  android:background="@drawable/rounded_alert"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp"
>
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rounded_alert"
  >

    <TableLayout
      android:stretchColumns="0"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
    >

      <TableRow>
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="left"
          android:textStyle="bold"
          android:text="Sorry, that's wrong!"
          android:textColor="@color/gray_dark" />

        <ImageView
          android:id="@+id/check"
          android:background="@drawable/xmark"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="right"
          android:layout_marginRight="10dp" />

      </TableRow>
    </TableLayout>

    <TextView
      android:id="@+id/alert_text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:ellipsize="none"
      android:text="In fact, this is where the explanation will go. Something about how this passage related to the topic"
      android:textColor="#000000" />

    <Button
      android:id="@+id/okay_button"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:layout_gravity="center_horizontal"
      android:background="@drawable/rounded_alert_button"
      android:text="Okay"
      android:textColor="@color/white"
      android:textSize="20sp" />
  </LinearLayout>
</LinearLayout>
下面是我的一个简短意思

谢谢


John

这并不完美,但我通过设置对话框相对于默认显示的布局来纠正它

dialog = new Dialog(this, R.style.AlertDialogTheme);
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(
    (int)(window.getWindowManager().getDefaultDisplay().getWidth()  * .90),
    (int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90 ));

dialog.setCancelable(false);
只要调整“.90”值,直到感觉正确为止。

以下是解决方案:

  • 您应该在对话框xml文件的外部添加Linearlayout
  • 然后将此线性布局的重心设置为“中心”
  • 最后一步是创建LayoutParams并将其设置为对话框

    android.view.WindowManager.LayoutParams lp=新的android.view.WindowManager.LayoutParams(); lp.width=android.view.WindowManager.LayoutParams.FILL\u父项; lp.height=android.view.WindowManager.LayoutParams.FILL\u父项; alertDialog.getWindow().setAttributes(lp)


  • 可能是您选择的
    样式
    中有一些大小限制,这会阻止您调整对话框的大小。试着不穿款式,看看是否有区别。带有
    layout\u weight
    LinearLayout
    TableLayout
    容易,为什么有两个嵌套的
    LinearLayout
    您只需要一个。尽可能消除像这样的“额外”视图,它们可能会干扰布局。在设置
    textSize
    时,我也无法调整
    按钮的大小。您可以尝试从
    按钮中删除填充。如果您的背景是9-patch,那么边距设置也可能存在问题。
    
    dialog = new Dialog(this, R.style.AlertDialogTheme);
    dialog.setContentView(layout);
    Window window = dialog.getWindow();
    window.setLayout(
        (int)(window.getWindowManager().getDefaultDisplay().getWidth()  * .90),
        (int)(window.getWindowManager().getDefaultDisplay().getHeight() * .90 ));
    
    dialog.setCancelable(false);