对话片段的活动始终在中间

时间:2019-06-22 19:01:17

标签: android android-layout android-dialogfragment dialogfragment

上面的图像是我当前的活动,其中包含一个对话框片段。如您所见,对话框位于屏幕的中间,但它希望它看起来像在Google地图中,当我单击按钮时,对话框出现在按钮附近:

enter image description here

=>

result

正如您在Google地图中看到的那样,该对话框不在所选按钮附近的中间。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/edit_name"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:orientation="vertical">

<TextView
    android:id="@+id/lbl_your_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your name" />

<EditText
    android:id="@+id/txt_your_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text" />
</LinearLayout>

看起来像这样:

res4e

如您所见,当我编辑对话框片段时,edittext和textview位于屏幕顶部,但是由于某些原因,当我部署应用程序时,它们位于中间。

1 个答案:

答案 0 :(得分:0)

尝试此操作,它将移动对话框BOTTOM,TOP,RIGHT,LEFT。

Dialog d = <code to create custom dialog>;

Window window = d.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM; 
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);

使用 Gravity.TOP,BOTTOM,RIGHT,LEFT 将对话框放置在该位置。