0

我想显示一个宽度应该适合平板电脑屏幕大小的对话框,谁能告诉我怎么做,我尝试设置宽度,但没有成功。

代码如下:

            final Dialog nagDialog = new Dialog(MainActivity.this);

            Window window = nagDialog.getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.gravity = Gravity.BOTTOM;
            wlp.width= WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(wlp);
            getWindow().setGravity(Gravity.BOTTOM);
            nagDialog.setContentView(R.layout.popuplayout);
            nagDialog.show();

popuplayout.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="84dp"
    android:layout_marginTop="59dp"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/img"
    android:layout_marginLeft="130dp"
    android:layout_toRightOf="@+id/img"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/img"
    android:layout_marginLeft="130dp"
    android:layout_toRightOf="@+id/img"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/img"
    android:layout_marginLeft="347dp"
    android:layout_toRightOf="@+id/img"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageButton5"
    android:layout_marginLeft="163dp"
    android:layout_toRightOf="@+id/imageButton5"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageButton4"
    android:layout_marginLeft="110dp"
    android:layout_toRightOf="@+id/imageButton4"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>
4

3 回答 3

0

dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

于 2013-11-06T07:07:19.887 回答
0

将父级Layout的宽度和高度设置为match_paren,即R.layout.popupplayout

于 2013-11-06T06:56:38.317 回答
0
final Dialog nagDialog = new Dialog(MainActivity.this);

        nagDialog.setContentView(R.layout.popuplayout);// note here!!!!

        Window window = nagDialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width= WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        getWindow().setGravity(Gravity.BOTTOM);

        nagDialog.show();

不要在使用 setContentView(R.layout.xxx) 方法之前设置属性。确保首先调用 setContentView(R.layout.xxx) 方法,然后更改对话框窗口的属性。

于 2014-07-04T02:58:29.793 回答