Android Dialogは右、左、中央にポップアップ

1261 ワード

DialogまたはAvtivityがDialog形式でポップアップされると、一般的にポップアップのデフォルトは中央になります.
しかし、私たちは時々右から飛び出して、ずっと右にいる必要があります.あるいは左から飛び出して、ずっと左にいます.
純Dialogのポップアップ位置を変更するには、次の手順に従います.
Dialog dialog = getDialog();
Window window = dialog == null ? null : dialog.getWindow();
if (dialog != null && window != null) {
    LayoutParams attr = window.getAttributes();
    if (attr != null) {
        attr.height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
        attr.gravity = Gravity.RIGHT;
    }
}

Activity Dialogの場合は、onCreate()メソッドで変更します.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().getAttributes().gravity = Gravity.RIGHT;
}