Android 片段与复合控件

发布于 2024-11-11 17:27:15 字数 254 浏览 9 评论 0原文

为什么要使用Android 3.0片段而不是复合控件?人们可以创建一个视图继承器或复合控件一次并在任何地方使用它。

我读过 http://android-developers.blogspot.com/2011/ 02/android-30-fragments-api.html 但没有找到答案。

Why should Android 3.0 fragments be used instead of compound controls? One can create a View inheritor or compound control once and use it everywhere.

I've read http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html but did not find the answer.

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

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

发布评论

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

评论(2

晨曦÷微暖 2024-11-18 17:27:15

不同之处在于片段有自己的生命周期(onPause、onCreate、onStart...)。通过具有生命周期,片段可以独立响应事件,通过 onSaveInstanceState 保存其状态,并返回(即,例如在来电后恢复或用户单击后退按钮时)。片段文档中总结了生命周期:

https://developer.android.com /guide/components/fragments.html#Lifecycle

您始终可以将片段或活动包装在复合视图周围,因此只需将片段视为复合视图的容器,从而为它们提供独立的生命周期。< /strong>

The difference is that fragments have a life cycle (onPause, onCreate, onStart...) of their own. By having a life cycle, fragments can respond independently to events, save their state through onSaveInstanceState, and be brought back (i.e. such as when resuming after an incoming call or when the user clicks the back button). The life cycle is summarized in the fragment documentation:

https://developer.android.com/guide/components/fragments.html#Lifecycle

You can always wrap a fragment or activity around a compound view, so just think of fragments as containers to your compound views that give them an independent life cycle.

川水往事 2024-11-18 17:27:15

原因是让相同的代码在平板电脑和手机上运行。这些设备有不同的布局注意事项,片段允许您考虑到这一点,并使您的应用程序表现不同,而无需重写任何代码。

The reason would be to have the same code work on tablets and phones. There are different layout considerations for these devices and Fragments allow you to take that into consideration and have your app behave differently without having to rewrite any code.

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