布局和片段布局之间的区别

布局和片段布局之间的区别

本文介绍了布局和片段布局之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建新的Android Activity时,它曾经在我要定义UI的layouts文件夹中创建了一个xml文件.现在,它创建了两个文件:

When creating an new Android Activity, it used to create an xml file in the layouts folder where I would define the UI.Now it creats two files:

1.Layout文件

1.Layout file

2.片段布局文件.

有人可以解释两者之间的区别吗?另外,当尝试将项目添加为列表视图,按钮等时.我应该在活动文件中将它们添加到哪个文件中.

Could someone explain the difference between the two? Also when trying to add items as listviews, buttons...etc. in which file should I add them to be called in my activity file.

推荐答案

从Android 3.0开始,活动"现在可以托管片段,这些片段可用于开发UI的各个部分,并根据屏幕大小,方向以不同的配置显示,以及其他因素.强烈建议在现代Android应用程序中使用Fragments,但这不是必需的.

Starting with Android 3.0, Activities may now host Fragments which can be used to develop portions of the UI, and be displayed in different configurations depending on screen size, orientation, and other factors. It is highly recommended to use Fragments in modern Android applications, but is not required.

您可以创建一个活动布局,该布局将容纳一个或多个片段,然后将您的UI组件放置在片段的布局中.活动将加载片段,然后片段将为您希望在其中展示的布局充气.您还可以根据希望向用户显示的内容动态地在同一活动内添加/删除/交换不同的片段.

You can create an Activity layout which will hold one or more Fragments, and then place your UI components in the Fragment's layout. The Activity will load the Fragment, and then the Fragment will inflate the layout you wish to present inside it. You can also dynamically add/remove/swap out different Fragments inside the same Activity, depending on what you wish to display to the user.

您可以在此处详细了解如何使用片段:片段|Android开发人员

You can read more about how to use Fragments here: Fragments | Android Developers

您还可以选择忽略片段"设计原理,并将所有布局继续放置到活动"布局文件中.在这种情况下,您可以删除Fragment布局.

You can also choose to ignore the Fragment design principle, and continue to place all of your layouts into the Activity layout file. In that case, you can delete the Fragment layout.

这篇关于布局和片段布局之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:54