gpt4 book ai didi

android - 为什么要在 Android 中使用 fragment ?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:51:56 25 4
gpt4 key购买 nike

很长一段时间以来,我一直在想,如果我只是为 Android Phone 开发应用程序而不是为 10.1 开发应用程序,那么在 Android 中使用 fragment 的原因是什么?

有必要用fragment吗?另外, fragment 的用法是什么,我发现它可能用于“选项卡”和“单独 View ”...

我真的觉得这很困惑。任何人都可以简要解释并举例说明吗?

最佳答案

来自文档

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

一些优点是..

  1. 一个特定的 UI 部分,一旦在 fragment 中完成,就可以在相同/不同的 Activity 。
  2. 您可以分隔 UI 的不同部分,因此代码会很整洁,并且易于阅读。

当您为不同类型的 Android 设备(手机、平板电脑)创建应用程序时, fragment 的重用能力非常有用。设计良好的 fragment 可以直接插入到您的 UI 层次结构中。

关于android - 为什么要在 Android 中使用 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13756999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com