Android 更改自定义对话框的位置

Android 更改自定义对话框的位置,android,Android,我正在开发一个应用程序,需要在位于导航栏的菜单按钮下方显示对话框,为了实现这一点,我使用了下面的代码 Dialog dialogMenu = new Dialog(mContext); dialogMenu.setContentView(R.layout.dialog_main_menu); dialogMenu.getWindow().setBackgroundDrawable( new ColorDrawable(android

我正在开发一个应用程序,需要在位于导航栏的菜单按钮下方显示对话框,为了实现这一点,我使用了下面的代码

Dialog dialogMenu = new Dialog(mContext);
        dialogMenu.setContentView(R.layout.dialog_main_menu);
        dialogMenu.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WindowManager.LayoutParams windowLayout = dialogMenu.getWindow()
                .getAttributes();
        windowLayout.gravity = Gravity.TOP | Gravity.LEFT;
        dialogMenu.getWindow().getAttributes().verticalMargin = -0.8F;
        dialogMenu.getWindow().getAttributes().horizontalMargin = 0.02F;
        dialogMenu.getWindow().setAttributes(windowLayout);
但问题是,在一个固定的垂直边距对话框并没有如图所示向上移动之后,实际上我希望对话框点应该触动菜单按钮。我找不到解决办法。

这是我的对话框\u main\u menu.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" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/main_menu_bg" >

            <TextView
                android:id="@+id/txtHeadingMenu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                android:text="@string/menu"
                android:textColor="@android:color/white"
                android:textSize="14sp" />

            <Button
                android:id="@+id/ListItem_btnMyItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtHeadingMenu"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/my_item"
                android:textColor="@android:color/white"
                android:textSize="13sp" />

            <Button
                android:id="@+id/ListItem_btnMyCategory"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ListItem_btnMyItem"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/my_category"
                android:textColor="@android:color/white"
                android:textSize="13sp" />

            <Button
                android:id="@+id/ListItem_btnPersonalList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ListItem_btnMyCategory"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/personal_list"
                android:textColor="@android:color/white"
                android:textSize="13sp" />

            <Button
                android:id="@+id/ListItem_btnStore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ListItem_btnPersonalList"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/store"
                android:textColor="@android:color/white"
                android:textSize="13sp" />

            <Button
                android:id="@+id/ListItem_btnSharedList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ListItem_btnStore"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="4dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/shared_list"
                android:textColor="@android:color/white"
                android:textSize="13sp" />

            <Button
                android:id="@+id/ListItem_btnSettings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ListItem_btnSharedList"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="4dp"
                android:layout_marginTop="4dp"
                android:background="@drawable/selector_btn_sort_dialog"
                android:text="@string/settings"
                android:textColor="@android:color/white"
                android:textSize="13sp" />
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

通过应用

windowLayout.x = (put values according to your requirement)  // and 
windowLayout.y=(put values according to your requirement) 
//and ignore using 
dialogMenu.getWindow().getAttributes().verticalMargin = -0.8F;
dialogMenu.getWindow().getAttributes().horizontalMargin = 0.02F;

你的布局是什么???您也可以使用quickaction访问@Jitesh Upadhyay:dialog_layout.xml是否有助于更改对话框的位置?您是否尝试过更改垂直边距?还可以尝试使用资源的布局边距进行洗牌。我也尝试过使用x和y参数,但在y值固定后,对话框不会向上移动。