【问题标题】:Show Android DialogFragment as dialog in portrait mode and as part of activity in landscape mode将 Android DialogFragment 显示为纵向模式下的对话框和横向模式下的活动的一部分
【发布时间】:2012-04-29 23:55:31
【问题描述】:

是否有可能实现某种“自我管理”的 DialogFragment,它在纵向模式下像 Dialog 一样显示其自身,在横向模式下像活动的一部分一样。如果你能给我一些代码,那就太好了

【问题讨论】:

  • 我也有类似的问题:How to display an full screen activity with Fragment in portrait, and the same Fragment as dialog in Landscape?

标签: android fragment


【解决方案1】:

类似这样的:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多