Question

Is it possible by using a DialogFragment to be able to move it out of the center and place it anywhere on the screen?

Était-ce utile?

La solution 2

I have success using

 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);

puts my dialog in the Top Right slightly down from the corner. this code is in onCreateDialog().

Autres conseils

You can use

getDialog().getWindow().setAttributes(param);

in

onCreateView()

like this

@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);
   .
   .
}

I suffered a lot trying all the programmatically solutions with no results. Finally I've done it from the XML file without any extra code within the Java class.

All I've done is Make the parent height match_parent and set a gravity for it with value center

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top