質問

ポップアップ画面のようにDialogFragmentを示すためのViewを持っています。 ウィンドウが画面の中央に常に表示されます。 DialogFragmentウィンドウの位置を設定する方法はありますか? ソースコードですが、まだ何かを見つけることができませんでした。

役に立ちましたか?

解決

このようなものを試してみてください:

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

またはgetDialog().getWindow()のその他の方法

セットコンテンツを呼び出した後の位置を必ず設定してください。

他のヒント

right、私はこれで最後にDialogFragmentが望んでいるように位置づける前に、1時間か2回の壁に向かいました。

私は Steelightの答えここにいます。これは私が見つけた最も単純で最も信頼できるアプローチです。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
    Window window = getDialog().getWindow();

    // set "origin" to top left corner, so to speak
    window.setGravity(Gravity.TOP|Gravity.LEFT);

    // after that, setting values for x and y works "naturally"
    WindowManager.LayoutParams params = window.getAttributes();
    params.x = 300;
    params.y = 100;
    window.setAttributes(params);

    Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y));
} 
.

params.widthparams.softInputMode(Steeightの回答で使用されている)はこれには無関係です。


下記P>はより完全な例です。私が本当に必要なのは、私の場合にimageButtonで「ソース」または「親」ビューの横にある「確認ボックス」ダイアログフレグネクションを揃えることでした。

カスタムフラグメントの代わりに、ダイアログフラグメントを使用することを選択しました。これにより、[ダイアログ]機能(ユーザーがその外側にクリックするとダイアログを閉じるなど)が表示されます。

configure configurebox
の例 "Source" ImageButton(Trashcan)

/**
 * A custom DialogFragment that is positioned above given "source" component.
 *
 * @author Jonik, https://stackoverflow.com/a/20419231/56285
 */
public class ConfirmBox extends DialogFragment {
    private View source;

    public ConfirmBox() {
    }

    public ConfirmBox(View source) {
        this.source = source;            
    }

    public static ConfirmBox newInstance(View source) {
        return new ConfirmBox(source);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(STYLE_NO_FRAME, R.style.Dialog);
    }


    @Override
    public void onStart() {
        super.onStart();

        // Less dimmed background; see https://stackoverflow.com/q/13822842/56285
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.2f; // dim only a little bit
        window.setAttributes(params);

        // Transparent background; see https://stackoverflow.com/q/15007272/56285
        // (Needed to make dialog's alpha shadow look good)
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Put your dialog layout in R.layout.view_confirm_box
        View view = inflater.inflate(R.layout.view_confirm_box, container, false);

        // Initialise what you need; set e.g. button texts and listeners, etc.

        // ...

        setDialogPosition();

        return view;
    }

    /**
     * Try to position this dialog next to "source" view
     */
    private void setDialogPosition() {
        if (source == null) {
            return; // Leave the dialog in default position
        }

        // Find out location of source component on screen
        // see https://stackoverflow.com/a/6798093/56285
        int[] location = new int[2];
        source.getLocationOnScreen(location);
        int sourceX = location[0];
        int sourceY = location[1];

        Window window = getDialog().getWindow();

        // set "origin" to top left corner
        window.setGravity(Gravity.TOP|Gravity.LEFT);

        WindowManager.LayoutParams params = window.getAttributes();

        // Just an example; edit to suit your needs.
        params.x = sourceX - dpToPx(110); // about half of confirm button size left of source view
        params.y = sourceY - dpToPx(80); // above source view

        window.setAttributes(params);
    }

    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }
}
.

必要に応じてコンストラクタパラメータやセッタを追加することで、上記の一般的な使用を可能にするのは非常に簡単です。 (My Final ConfirmBoxには、TextとView.OnClickListenerがコードでカスタマイズできるスタイルのボタン(一部の境界など)があります。)

getDialog().getWindow()は、Hostingアクティビティが表示されていない場合はNULLを返します。これはフラグメントベースのアプリを書いている場合でもないため、DialogFragmentはNULLを返します。getWindow()を試したときにNullPointerExceptionが表示されます。

私はMobistionの答えをお勧めします。ダイアログフレグメントクラスがすでにある場合は、切り替えるには難しくありません。OnCreatedIalogメソッドを構築してPopUpwindowを返します。その後、getAttributes()に提供するビューを再利用して、AlertDialog.builder.setView()を呼び出してください。

次のようなダイアログフレグメンメントにOnResume()メソッドをオーバーライドする必要があります。

@Override
public void onResume() {
    final Window dialogWindow = getDialog().getWindow();
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
    lp.x = 100;        // set your X position here
    lp.y = 200;        // set your Y position here
    dialogWindow.setAttributes(lp);

    super.onResume();
}
.

ビューの場所とサイズを指定できるため、この目的のためにPopupWindowクラスを使用しています。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(mActivity, R.style.BottomDialog);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 
    dialog.setContentView(R.layout.layout_video_live);
    dialog.setCanceledOnTouchOutside(true);

    Window window = dialog.getWindow();
    assert window != null;
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.gravity = Gravity.BOTTOM; //psotion
    lp.width = WindowManager.LayoutParams.MATCH_PARENT; // fuill screen
    lp.height = 280;
    window.setAttributes(lp);
    return dialog;
}
.

AppCompatDialogFragmentからandroid.support.v7.app.AppCompatDialogFragmentを使用しています。

だから、私はこれから望んでいました(黄色の背景はダイアログフラグメントのrootlayoutから来ています):

 SRC_IMG_1

これを取得します:

href="https://i.stack.imgur.com/ho9fz.png" rel="nofollow noreferrer">  SRC_IMG_2

が不合理されたソリューションのどれも仕事をしなかった。だから、私はこれを行うことができました:

fun AppCompatDialogFragment.alignToBottom() {
    dialog.window.apply {
        setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL)
        decorView.apply {

            // Get screen width
            val displayMetrics = DisplayMetrics().apply {
                windowManager.defaultDisplay.getMetrics(this)
            }

            setBackgroundColor(Color.WHITE) // I don't know why it is required, without it background of rootView is ignored (is transparent even if set in xml/runtime)
            minimumWidth = displayMetrics.widthPixels
            setPadding(0, 0, 0, 0)
            layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            invalidate()
        }
    }
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top