在纵向模式下将Android DialogFragment显示为对话框,并在横向模式下显示为活动的一部分

Jur*_*ius 6 android fragment

是否可以实现某种"自我管理"的DialogFragment,它在纵向模式下显示像Dialog,并且在横向模式下显示活动的一部分.如果你给我提供一些代码,那将是非常重要的

jt-*_*son 0

像这样的东西:

Activity activity = getActivity();
int orientation = activity.getResources().getConfiguration().orientation;

if (orientation == Configuration.ORIENTATION_LANDSCAPE)
{
    // Start DialogFragment as dialog
    MyFragmentDialog frag = new MyFragmentDialog();
    frag.show(getFragmentManager(), "dialog");
}
else
{
    // Start activity that embeds DialogFragment
    Intent intent = new SimpleFragmentActivity.IntentBuilder(activity, MyFragmentDialog.class)
            .create();
    activity.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

SimpleFragmentActivity是我编写的一个包装器活动,为了方便起见,它只是将片段对话框嵌入到活动中,但基本上您只需要一个在其中嵌入 MyFragmentDialog 的活动。这是包装器活动的来源:https ://github.com/jt-gilkeson/fragment-utils