为什么我的对话框不占用整个屏幕宽度

时间:2013-10-03 08:26:01

标签: android dialog

我正在使用自定义对话框在屏幕上显示内容

绿色部分是对话框 - 它应该是整个屏幕宽度

为什么不占用整个屏幕宽度?

这是我的对话框代码

        participantsPanelDialog = new Dialog(getSherlockActivity(), R.style.ParticipantsDialog);
        // Making sure there's no title.
        participantsPanelDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Making dialog content transparent.
        participantsPanelDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // Removing window dim normally visible when dialog are shown.

        participantsPanelDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        participantsPanelDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        // Setting position of content, relative to window.
        WindowManager.LayoutParams params = participantsPanelDialog.getWindow().getAttributes();
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 0;
//      params.width = ((Activity) getActivity()).getWindow().getAttributes().width;
//      Toast.makeText(getActivity(), "width: " +params.width, Toast.LENGTH_LONG).show();
        participantsPanelDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

这是我的布局

<?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="wrap_content" >

    <RelativeLayout
        android:id="@+id/topPanelOpen"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@drawable/top_pannel_open"
        android:orientation="horizontal"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="5dp" >

        <RelativeLayout
            android:id="@+id/lytOneParticipant"
            android:layout_width="match_parent"
            android:layout_height="145dp"
            android:visibility="gone" >

            <RelativeLayout
                android:id="@+id/lytInfoOneParticipant"
                android:layout_width="match_parent"
                android:layout_height="@dimen/abs__action_bar_default_height"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/ivPeopleIcon"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_gravity="left"
                    android:scaleType="centerCrop"
                    android:src="@drawable/ic_people_normal" />

                <TextView
                    android:id="@+id/tvParticipantName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/ivPeopleIcon"
                    android:text="ParticipantName"
                    android:textColor="@android:color/white" />

                <ImageView
                    android:id="@+id/btnAddParticipants"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/btnClose"
                    android:gravity="center"
                    android:onClick="onParticipantsMenuItemSelected"
                    android:paddingRight="12dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_add_normal" />

                <ImageView
                    android:id="@+id/btnClose"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:onClick="onParticipantsMenuItemSelected"
                    android:paddingLeft="12dp"
                    android:src="@drawable/ic_close_normal" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/lytInfoOneParticipant" >

                <ImageView
                    android:id="@+id/leftArrow"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:paddingRight="16dp"
                      android:onClick="onParticipantsMenuItemSelected"
                    android:src="@drawable/ic_arrow_left_normal" />

                <ImageView
                    android:id="@+id/avatar"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_above="@+id/itemCount"
                    android:layout_toRightOf="@+id/leftArrow" />

                <TextView
                    android:id="@+id/itemCount"
                    android:layout_width="60dp"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/avatar"
                    android:layout_alignParentBottom="true"
                    android:gravity="center_horizontal"
                    android:text="TEXT"
                    android:textColor="@android:color/white"
                    android:textSize="24sp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignBottom="@+id/avatar"
                    android:layout_alignTop="@+id/avatar"
                    android:layout_marginLeft="5dp"
                    android:layout_toLeftOf="@+id/rightArrow"
                    android:layout_toRightOf="@+id/avatar"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/tvEmail"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="1dp"
                        android:layout_weight="1"
                        android:background="@drawable/white_round_corners"
                        android:gravity="center_vertical"
                        android:paddingLeft="5dp"
                        android:text="Email Address"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/tvName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:layout_marginTop="1dp"
                        android:layout_weight="1"
                        android:background="@drawable/white_round_corners"
                        android:gravity="center_vertical"
                        android:paddingLeft="5dp"
                        android:text="Full Name"
                        android:textSize="18sp" />
                </LinearLayout>

                <ImageView
                    android:id="@+id/rightArrow"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:paddingBottom="1dp"
                    android:paddingLeft="16dp"
                      android:onClick="onParticipantsMenuItemSelected"
                    android:src="@drawable/ic_arrow_right_normal" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/lytAllParticipants"
            android:layout_width="match_parent"
            android:layout_height="145dp"
            android:orientation="horizontal"
            >

            <LinearLayout
                android:id="@+id/lytInfo"
                android:layout_width="match_parent"
                android:layout_height="@dimen/abs__action_bar_default_height"
                android:layout_toLeftOf="@+id/lytControls"
                android:orientation="horizontal" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/tvNumPhotosInAlbum"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="top"
                        android:gravity="top"
                        android:text="645"
                        android:textColor="@android:color/white"
                        android:textSize="22sp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />

                    <TextView
                        android:id="@+id/tvNumPhotosInAlbumText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:gravity="bottom"
                        android:text="@string/numPhotosInAlbumText"
                        android:textColor="@android:color/white" />
                </LinearLayout>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/divider" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                        <ImageView
                            android:id="@+id/ivPeopleIcon"
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:layout_gravity="center_horizontal"
                            android:scaleType="centerCrop"
                            android:src="@drawable/ic_people_normal" />

                        <TextView
                            android:id="@+id/tvPeopleSharing"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="top"
                            android:gravity="top"
                            android:includeFontPadding="false"
                            android:paddingLeft="5dp"
                            android:text="5"
                            android:textColor="@android:color/white"
                            android:textSize="24dp" />
                    </LinearLayout>

                    <TextView
                        android:id="@+id/tvPeopleSharingText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:gravity="bottom"
                        android:text="@string/numPeopleSharingText"
                        android:textColor="@android:color/white" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lytControls"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:layout_alignParentRight="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/btnEditAlbum"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="onParticipantsMenuItemSelected"
                    android:src="@drawable/selector_settings" />

                <ImageView
                    android:id="@+id/btnAddParticipants"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:onClick="onParticipantsMenuItemSelected"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_add_normal" />
            </LinearLayout>

            <com.devsmart.android.ui.HorizontalListView
                android:id="@+id/participantsListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lytInfo"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@+id/lytControls" />
        </RelativeLayout>
        <!-- this view opens the panel do NOT delete it -->

        <View
            android:id="@+id/openPanel"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="24dp"
            android:alpha="0.4" />
    </RelativeLayout>

</RelativeLayout>

这就是结果:

enter image description here

4 个答案:

答案 0 :(得分:2)

我的问题是,当我创建对话框时,我正在扩展Android.Theme.Dialog样式

使它缩小

在这种风格R.style.ParticipantsDialog中 - 我删除了父主题,它变得像我想要的那样全尺寸

答案 1 :(得分:1)

Dialog可以通过这样做占用你想要的所有宽度:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
...
participantsPanelDialog.getWindow().setLayout(width, LayoutParams.WRAP_CONTENT);

顺便说一句,Dialog是一个&#34;窗口&#34;并且我不认为是为了占据屏幕上的所有可用空间而设计的。

答案 2 :(得分:0)

尝试以下代码

    int DisplayWidth, DisplayHeight;
    Dialog dialog;

   Display display =((WindowManager)Your_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

            DisplayWidth = display.getWidth();
            DisplayHeight = display.getHeight();

            dialog = new Dialog(activity_context);

              // Set your dialog width and height dynamically as per your screen.

            Window window = dialog.getWindow();
            window.setLayout(DisplayWidth , DisplayHeight);
            window.setGravity(Gravity.CENTER);

            dialog.show();

答案 3 :(得分:0)

<style name="ThemeDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
</style>

将上面的内容添加到style.xml中,并将对话框样式设置为ThemeDialog。