我可以删除 layout.xml 文件中定义的片段吗? [英] Can I remove a fragment defined in a layout.xml file?

查看:48
本文介绍了我可以删除 layout.xml 文件中定义的片段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 FragmentTransaction 和 remove() 方法来删​​除 layout.xml 中定义的片段(使用片段标记)?

Is is possible to use FragmentTransaction and the remove() method to get rid of fragments that are defined in the layout.xml (using the fragment tag) ?

我没有使用支持库 v4.0 让它工作.在您提交 FragmentTransaction 之后,在调用 remove() 之后,该片段将保持原位.谁能告诉我这是设计使然,错误还是功能?

I did not get this to work using the support libraries v4. The fragment stays in place after you commit the FragmentTransaction, after calling remove(). Can anyone tell me if this is by design, a bug or a feature?

可以替换lyaout.xml中定义的片段,所以我觉得有点奇怪,不应该删除它?

It is possible to replace a fragment that is defined in the lyaout.xml, so I find it a bit strange that it should not be possible to remove it?

推荐答案

Honeycomb 中可用的本机 API 与支持库中的 API 工作方式相同,因此您无法删除已在布局中声明的 Fragment 实例XML 文件.

The native APIs available starting in Honeycomb work the same as those in the support libarary, so you cannot remove an instance of a Fragment which has been declared in your layout XML file.

通过 FragmentTransactions,您可以操作 ViewGroups,例如 LinearLayouts,它们充当容器来保存其他 Fragment 的布局.但是,当您在布局中声明 Fragment 时,它没有相同意义上的容器,因为它是 View 层次结构的永久组成部分,因此您无法删除它.这是设计使然,以支持您永远不会删除的导航片段之类的东西.:)

With FragmentTransactions you manipulate ViewGroups such as LinearLayouts that act as containers to hold the layout of other Fragments. However, when you declare a Fragment in your layout, it doesn't have a container in the same sense because it is permanently part of the View hierarchy, so you can't remove it. That is by design, to support things like navigation Fragments that you'd never remove anyways. :)

一件很有趣的事情,我完全是偶然发现的,就是你可以将新的 Fragment 添加到在布局中用标签声明的 Fragment 中;它充当其他 Fragment 的容器

One thing that's interesting, and I found it out totally by accident, is that you can add new Fragments into a Fragment that was declared with the tag in your layout; and it acts as a container for other Fragments

这篇关于我可以删除 layout.xml 文件中定义的片段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆