[英]For supporting different screen size why should i use fragment in android instead of activity
I am beginner to android..I am started new android project..for supporting different screen size..in fragment documentation they given to use fragment..but why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings? 我是android的初学者。.我开始了新的android项目。为了支持不同的屏幕尺寸,在片段文档中给出了使用fragment ..但是为什么我不能在android中使用活动。我应该在这两者中使用哪个..请不要给出活动或片段的链接..请任何人回答我..我不知道该使用哪个?...我想要他们给出的有关活动和片段的所有文档,但我要了解使用哪个..以下是我阅读的有关片段的链接..如果我使用活动,我应该做更多的编码吗?
https://developer.android.com/guide/components/fragments.html https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity. 实际上,您不能单独使用片段,片段在Activity内部。 One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
使用片段来支持不同的屏幕尺寸的一点是能够实现某些视图,例如“主/详细”视图。 A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
片段,如它的名字一样,是一个更大的控制器“活动”的一部分 ,其引用可以被删除,它比有很大的大规模活动来处理视图的所有状态的清洁剂。
So the use case is completely depends on your project and its User Interface. 因此,用例完全取决于您的项目及其用户界面。 I'd be glad to help you if you give me more information about your project and its design.
如果您能向我提供有关您的项目及其设计的更多信息,我们将很高兴为您提供帮助。
I think you will need at least one activity. 我认为您至少需要一项活动。 And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
然后,为了更好地处理不同的设备旋转和屏幕尺寸,您可以在此活动中使用一个或多个片段。
I try to explain this with an example: You want to create a nice music player app which should look nice in portrait and landscape mode. 我尝试用一个例子来解释这一点:您想创建一个漂亮的音乐播放器应用程序,该应用程序在纵向和横向模式下看起来都不错。
You split your app up into three fragments: 您将应用程序分为三个部分:
Here you can see how the app looks in portrait mode. 在这里,您可以查看应用在纵向模式下的外观。 The activity shows two fragments: The first fragment only consists of a listview.
该活动显示两个片段:第一个片段仅包含一个列表视图。 There the song titles are listed.
那里列出了歌曲标题。 On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
在底部,您可以看到第二个片段,其中显示了当前播放歌曲的歌曲标题,并有一个用于暂停音乐的按钮。
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. 当您的用户以横向模式在平板电脑上使用音乐播放器应用时,您将有更多空间来显示内容。 Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (eg the album image) and a progress bar.
然后,活动显示列表片段(也将以纵向模式显示),并显示第三个片段,该片段显示有关当前播放歌曲的详细信息(例如专辑图像)和进度条。
By using fragments you only need to write the code for the list once. 通过使用片段,您只需要为列表编写一次代码。
Sweet and Simple thing, What i recommend is always use Fragments , But for Fragment you will require Activity . 很简单,我建议始终使用Fragments ,但是对于Fragment,您将需要Activity 。
For more info: Here is the most accepted answer for this topic. 有关更多信息: 这是该主题最被接受的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.