【问题标题】:Floating Fragment浮动片段
【发布时间】:2016-03-14 19:58:59
【问题描述】:

我正在尝试通过充气并使用 FragmentTransaction 将其添加到 Activity 来动态创建浮动片段。

当我将它添加到容器中(添加到我在容器中为它创建的 FrameLayout 中)时,它下面的所有部分都被向下推,并且所有视图都发生了变化(我希望视图在有或没有的情况下看起来都一样片段)。

我查看了 DialogFragment 的实现,它浮动并且不改变视图,我看到它使用: FragmentTransaction.add(int containerViewId...) 方法,其中 0 作为 containerViewId。 在谷歌文档中,它只是写道:“如果为 0,它将不会被放置在容器中”。当我在 containerViewId 中放入 0 并从视图中删除 FrameLayout 时,Fragment 没有出现。

【问题讨论】:

  • 你应该只使用 [android-fragments]。不要将[fragment] 用于android 问题...如果您只使用一个,人们将更有可能看到您的问题,相信我:)

标签: android android-fragments


【解决方案1】:

我认为您面临的问题来自您将浮动片段添加到 FrameLayout 的事实,该片段是已经存在的布局的子元素,正如您已经看到的那样,它将修改当前布局以腾出空间对于新片段。例如,如果您当前的布局是这样的(这只是一个示例):

<LinearLayout>
    <FrameLayout android:id="@+id/here_add_floating_fragment"/>
    <the rest of the layout file with the content that should hold its position />
</LinearLayout>

如果浮动片段被添加到FrameLayout 中,就像上面的布局一样,那么其余的视图确实会被移动以腾出空间。

为防止这种情况,您必须修改布局,以便 FrameLayout 将保持您当前的布局,如下所示:

<FrameLayout android:id="@+id/here_add_floating_fragment"> 
      <LinearLayout>
          <the rest of the layout file with the content that should hold its position />
      </LinearLayout>
</FrameLayout> 

现在,当您添加新片段时,当前的Views 将保留在原处,片段将作为FrameLayout 堆栈其子级出现在上方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2015-06-25
    相关资源
    最近更新 更多