如何使用片段在显示的图像中创建布局

发布于 2024-12-23 00:14:10 字数 190 浏览 2 评论 0原文

这是图像屏幕截图:

在此处输入图像描述

我的问题是如何实现所示的堆栈布局?我已经搜索了资源文件,大多数只展示了如何实现两个片段。

有人可以给我一些如何创建如图所示的堆栈片段的示例吗?

会感激的。谢谢。

Here is the image screenshot:

enter image description here

My query is how can I implement the Stack layout shown? I have scoured the resource files and most only show how to implement two fragments.

Would anyone please give me any example of how I can create the Stack fragment as shown?

Will appreciate it. Thanks.

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

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

发布评论

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

评论(2

﹉夏雨初晴づ 2024-12-30 00:14:10

如果您的片段紧密相连,请不要在单独的活动中运行它们。对于单窗格情况,您只需在一个活动中切换片段即可。

否则,如果您确实想在活动之间将它们分开,则需要使用 onActivityResult() 模型来传播结果,并且在双窗格情况下,只需对第一个片段进行第二次调用 onActivityResult() 即可“模拟”它。请注意,Fragment.setTargetFragment() 包含一个请求代码参数来促进这一点。

I have activity A loading Fragment F1 which loads fragment F2

恕我直言,片段不应加载其他片段。活动根据可用的屏幕空间加载片段。片段不应该知道也不关心当前活动中是否存在任何其他片段,或者其他活动中是否存在其他片段。

I have F2 calling back to activity A to pop it off of the stack. Should I then be looking at passing a message to F1 to do what it needs to do?

是的。

What I'm wondering is whether I'm on the right track with regards passing messages back and forwards via the parent activity or is there a more direct way of F1 responding to F2 performing something that requires it to be closed and F1 do what it needs to do.

我什至不会让 F1 知道 F2 的存在,反之亦然。当用户在 F1 中执行的某些操作应导致主要上下文转换(例如,显示某些其他片段/活动)时,F1 应让托管活动知道,也许通过向 F1 注册的侦听器接口(以支持多个可能的托管活动) 。然后,该活动将安排 F2 出现在其自己的活动中或另一个活动中。类似地,当 F2 结束时,它会通过侦听器接口告知其托管活动,并且该活动可以将控制路由回适当的位置。

我对您的整个“F2 执行应导致其关闭的操作”方法有些怀疑,除非 F2 是 DialogFragment。

If your fragments are closely tied together, just don't run them in separate activities. For the single pane case you can just switch fragments within one activity.

Otherwise, if you do want to separate them between activities, you need to use the onActivityResult() model for propagating results back, and in the dual-pane case "emulate" it by just having the second call onActivityResult() of the first fragment. Note that Fragment.setTargetFragment() includes a request code argument to facilitate this.

I have activity A loading Fragment F1 which loads fragment F2

IMHO, fragments should not load other fragments. Activities load fragments, based upon available screen space. Fragments should neither know nor care whether any other fragment exists in the current activity, or if other fragments are in other activities.

I have F2 calling back to activity A to pop it off of the stack. Should I then be looking at passing a message to F1 to do what it needs to do?

Yes.

What I'm wondering is whether I'm on the right track with regards passing messages back and forwards via the parent activity or is there a more direct way of F1 responding to F2 performing something that requires it to be closed and F1 do what it needs to do.

I would not have F1 even know that F2 exists, or vice versa. When the user does something in F1 that should result in a major context shift (e.g., display some other fragment/activity), F1 should let the hosting activity know, perhaps via a listener interface registered with F1 (to support multiple possible hosting activities). The activity would then arrange for F2 to appear, either in its own activity or in another activity. Similarly, when F2 wraps up, it would let its hosting activity know via a listener interface, and that activity can route control back to the appropriate spot.

I am somewhat skeptical of your whole "F2 performs an action which should result in it being closed" approach, unless F2 is a DialogFragment.

墨洒年华 2024-12-30 00:14:10

下面给出了一种可能的解决方案
请根据使用情况调整宽度和高度

对于横向使用

<LinearLayout android:orientation="horizontal" ..>
   <fragment android:name="com.example.FragmentA"
        android:id="@+id/fa"
        android:layout_width="xdp"
        android:layout_height="match_parent" />
  <LinearLayout android:orientation="vertical" ...>
      <fragment android:name="com.example.FragmentB"
        android:id="@+id/fb"
        android:layout_width="ydp"
        android:layout_height="wrap_content" />
      <fragment android:name="com.example.FragmentC"
        android:id="@+id/fc"
        android:layout_width="zdp"
        android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>

对于纵向视图使用

<LinearLayout android:orientation="vertical" ..>
   <fragment android:name="com.example.FragmentA"
        android:id="@+id/fa"
        android:layout_width="match_parent"
        android:layout_height="Xdp" />
  <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
      <fragment android:name="com.example.FragmentB"
        android:id="@+id/fb"
        android:layout_width="ydp"
        android:layout_height="wrap_content" />
      <fragment android:name="com.example.FragmentC"
        android:id="@+id/fc"
        android:layout_width="wrap_cotent"
        android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>

One possible solution is given below
Please tweak width and heights according to the use case

For Landscape use

<LinearLayout android:orientation="horizontal" ..>
   <fragment android:name="com.example.FragmentA"
        android:id="@+id/fa"
        android:layout_width="xdp"
        android:layout_height="match_parent" />
  <LinearLayout android:orientation="vertical" ...>
      <fragment android:name="com.example.FragmentB"
        android:id="@+id/fb"
        android:layout_width="ydp"
        android:layout_height="wrap_content" />
      <fragment android:name="com.example.FragmentC"
        android:id="@+id/fc"
        android:layout_width="zdp"
        android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>

For Portrait view use

<LinearLayout android:orientation="vertical" ..>
   <fragment android:name="com.example.FragmentA"
        android:id="@+id/fa"
        android:layout_width="match_parent"
        android:layout_height="Xdp" />
  <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
      <fragment android:name="com.example.FragmentB"
        android:id="@+id/fb"
        android:layout_width="ydp"
        android:layout_height="wrap_content" />
      <fragment android:name="com.example.FragmentC"
        android:id="@+id/fc"
        android:layout_width="wrap_cotent"
        android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文