为什么我应该在 Android 中使用片段? [英] Why should I use fragment in Android?

查看:37
本文介绍了为什么我应该在 Android 中使用片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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吗?还有,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).

一些优点是..

  1. 一个特定的 UI 部分,一旦在片段中完成,就可以在相同/不同的活动.
  2. 您可以将 UI 的不同部分分开,因此代码会很整洁,并且易于阅读.

fragment 能够重用的能力在您为不同类型的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆