我可以删除在layout.xml 文件中定义的片段吗?

发布于 2024-12-27 16:36:11 字数 222 浏览 2 评论 0原文

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

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

可以替换 lyaout.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) ?

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2025-01-03 16:36:11

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

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

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

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.

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. :)

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

萌吟 2025-01-03 16:36:11

就像@david-c-sainte-claire 和@martín-marconcini 所说,您不能使用remove() 方法和FragmentTransaction 来删除XML 中定义的片段。这并不意味着你不走运。您始终可以使用 setVisibility() 方法。

findViewById(R.id.fragment_main).setVisibility(View.GONE);

Like @david-c-sainte-claire and @martín-marconcini said, you can't use remove() method and FragmentTransaction to remove the fragment that was defined in the XML. That doesn't mean you are out of luck. You can always use setVisibility() method.

findViewById(R.id.fragment_main).setVisibility(View.GONE);
向日葵 2025-01-03 16:36:11

我没有使用支持库 v4 来工作。片段
在提交 FragmentTransaction 后、调用后保持在原位
消除()。谁能告诉我这是设计使然、错误还是功能?

这是设计使然(或者缺乏某个功能,如果你问我的话,这绝对不是一个功能:P)。所以只要你使用支持库,你就无法实现这一点。

I did not 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?

This is by design (or a lack of a feature, not definitely a feature if you ask me :P). So as long as you are using the support libraries, you can't achieve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文