为什么要在Android中使用片段? [英] Why should I use fragment in Android?
问题描述
很长时间以来,我认为如果仅针对Android Phone
而不是针对10.1
开发应用程序,那么在Android中使用片段的原因是什么?
For very long time, I think what is the reason of using fragment in Android if I just develop the application for Android Phone
only but not 10.1
.
是否需要使用片段?还有,fragment的用法是什么 我发现它可能用于选项卡"和单独的视图" ...
Is it necessary to use fragment? Also, what is the usage of fragment, I found that it may use for 'tab' and 'separate view'...
我真的觉得这很令人困惑.谁能简要解释并举例说明?
I really think it is very confusing. Can anyone explain briefly and give example?
推荐答案
来自文档
您可以将片段视为活动的模块化部分, 有自己的生命周期,接收自己的输入事件,并且您可以 可以在活动运行时添加或删除(类似于 活动",您可以在其他活动中重复使用).
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).
一些优点是..
- 一个特定的UI部分,一旦完成片段处理后,就可以在其中重用 相同/不同的活动.
- 您可以将UI的不同部分分开,因此代码会很整洁, 且易于阅读.
- A particular UI part, once done in fragment, can be reused in same/different activities.
- You can separate different sections of UI, hence code will be neat, and easy readable.
在为不同类型的android设备(手机,平板电脑)创建应用程序时,片段能够重用的功能非常有用.设计良好的片段可以直接插入您的UI层次结构中.
The ability of fragment to be able to reuse is very helpful when you are creating applications for different kind of android devices (phones, tablets). A well designed fragment can be just plugged into your UI hierarchy.
这篇关于为什么要在Android中使用片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!