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" />