如何为10.1英寸平板电脑的全宽显示对话框

时间:2013-11-06 06:29:20

标签: android dialog

我想显示一个宽度应该适合平板电脑屏幕尺寸的对话框,任何人都可以让我怎么做,我尝试设置宽度,但没有成功。

代码如下:

            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>

3 个答案:

答案 0 :(得分:0)

将父Laoyout的宽度和高度设置为match_paren,即R.layout.popuplayout

答案 1 :(得分:0)

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

答案 2 :(得分: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)方法不要 setAttributes 之前。 确保首先调用setContentView(R.layout.xxx)方法,然后更改对话窗口的属性。