Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将DialogFragment移出中心?_Android_Fragment_Dialogfragment - Fatal编程技术网

Android 如何将DialogFragment移出中心?

Android 如何将DialogFragment移出中心?,android,fragment,dialogfragment,Android,Fragment,Dialogfragment,是否可以使用对话框片段将其移出中心并放置在屏幕上的任何位置 我成功地使用了 Window window = dialog.getWindow(); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.gravity = Gravity.TOP | Gravity.RIGHT; lp.x = 100; lp.y = 100; window.setAttributes(lp); 将我的对话框从右上角稍

是否可以使用
对话框片段
将其移出中心并放置在屏幕上的任何位置

我成功地使用了

 Window window = dialog.getWindow();
 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
 lp.gravity = Gravity.TOP | Gravity.RIGHT;
 lp.x = 100;
 lp.y = 100;
 window.setAttributes(lp);
将我的对话框从右上角稍微向下放置。此代码位于您可以使用的
onCreateDialog()

getDialog().getWindow().setAttributes(参数)

像这样

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
    WindowManager.LayoutParams param = getDialog().getWindow().getAttributes();
    param.width = LayoutParams.MATCH_PARENT;
    param.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
    param.x = 100;
    param.y = 100;
   .
   .
    getDialog().getWindow().setAttributes(p);
   .
   .
}

我尝试了很多编程解决方案,但都没有结果。最后,我从XML文件中完成了这项工作,在Java类中没有任何额外的代码

我所做的就是使父项高度
与父项匹配
并为其设置一个
重力
中心

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!--The following two lines-->
android:layout_height="match_parent"
android:gravity=“center"
android:orientation="vertical">

<Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/hide"
    android:textColor="@android:color/holo_blue_dark" />

<Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/cancel"
    android:textColor="@android:color/holo_blue_dark" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<!--The following two lines-->
android:layout_height="match_parent"
android:gravity=“center"
android:orientation="vertical">

<Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/hide"
    android:textColor="@android:color/holo_blue_dark" />

<Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/cancel"
    android:textColor="@android:color/holo_blue_dark" />