将片段放入另一个片段的问题

时间:2011-09-06 14:50:38

标签: android fragment

我尝试通过FragmentManager和FragmentTransaction(来自android.support.v4.app)将片段放入另一个片段内的FrameLayout中。容器片段在顶部有一个按钮和一个TextView,在底部有一个FrameLayout(我以编程方式创建布局,我不想用所有这些伤害你的眼睛)。 CreateView()工作得很好,我可以访问容器底部的FrameLayout,并根据需要动态添加或删除View

@Override
public void onClick(View button) {      
    FrameLayout frame = (FrameLayout)findViewById(DETAIL_CONTENT_FRAME);
    ImageView im = new ImageView(this);
    im.setImageResource(R.drawable.test);
    frame.addView(im);
}

但是当我尝试将一个片段而不是一个ImageView添加到frameLayout时,代码完全编译,但是在调用onClickListener()方法之后不会出现所需的片段。我检查了片段的onCreateView()方法,它返回了一个合适的视图...

@Override
public void onClick(View button) {
    ServerDialogFragment serverDialog = new ServerDialogFragment();
    FragmentTransaction addDialog = getSupportFragmentManager().beginTransaction();
    addDialog.add(DETAIL_CONTENT_FRAME, serverDialog);
    addDialog.commit();
}

你有答案吗?

PS:我曾经尝试过将片段添加到其他片段中并且它有效,但它们只是仅包含ImageViews的简单片段。

1 个答案:

答案 0 :(得分:1)

目前不支持其他片段内的片段。参见: