动态添加多个片段

发布于 2024-12-12 07:21:07 字数 367 浏览 5 评论 0原文

我正在考虑一个应用程序,在其中我将能够根据我存储在某处的元数据以编程方式显示一些片段。到目前为止,我已经发现,每个片段都位于相应的 FrameLayout 中,或者特别是,当我使用一个 FrameLayout 创建活动时,我一次只能存储一个片段,无论它是什么类型。然而,问题是,在这种情况下,当我的元数据声明我必须将 3 个片段放入我的活动中,而只有一个 FrameLayout 时。

我看到两种可能的解决方案:

1)制作几个FrameLayout,在最后阶段,其中一些将被使用或不使用 2)以某种方式连接多个片段以适合一个可用的 FrameLayout

我不喜欢解决方案 1)并且我不知道如何实现 2)。你怎么看?是否可以使用一个框架布局动态地将多个片段添加到活动中?

I am thinking of an app in which I will be programatically able to display some fragments according to metadata I have stored somewhere. Up to know, I have been able to find out, that each fragments lies in corresponding FrameLayout, or especially, when I create activity with one FrameLayout, I am able to store there only one Fragment at a time, no matter what kind is it. Problem is, however, with situation, when my metadata declares I have to put 3 fragments into my activity, while there is only one FrameLayout.

I see two possible solutions:

1) making several FrameLayout and in final stage, some of them will be used or not
2) somehow join multiple fragments to fit into one available FrameLayout

I don't like solutuion 1) and I don't know how to achieve 2). How to you see it? Is it possible to dynamically add multiple Fragments into activity with one Frame Layout?

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

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

发布评论

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

评论(1

南七夏 2024-12-19 07:21:08

在您需要将更多数量的片段添加到屏幕的情况下,避免使用 FrameLayout,您将无法实现它。有几个因素决定了如何布局多个片段

  1. 所有片段的大小是否相同?
  2. 片段应该按特定顺序放置吗?

首先创建一个空的父布局(除了FrameLayout之外,布局类由您选择)。接下来使用单独的 xml 文件定义父布局的子视图,该文件必须包含片段占位符。使用充气器充气您的子视图并将片段分配给充气布局并最终将其添加到您的父布局中,通过这种方式您可以激活您正在寻找的内容。请记住,当您扩展布局时,请务必提及其布局大小,以便实现您想要的布局。

In your situation where you require more number of Fragments to be added to your screen avoid using the FrameLayout, you will not be able to achive it. There are several factors that define how you layout multiple Fragments

  1. Are all the fragments of the same size?
  2. Should the fragments be place in a particular order?

First Create an empty parent layout (Layout class is left to your choice except FrameLayout). Next define your child view for your parent layout using a seperate xml file this file must contain your fragment place holder. Using the inflator inflate your child view and assign a Fragment to the inflated layout and eventually add it to your parent layout, through this way you can active what you are looking for. Just remember as you inflate your layout do mention its layout size so that you achive the kindly of layout you want.

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