[英]Can one class in android studio handle several different in layout and purpose XML layouts
Currently when I make a new layout I create a new class that handles that layout and then make a method to communicate with a class that the layout might communicate with, but can I use one class for everything. 当前,当我制作一个新的布局时,我创建了一个处理该布局的新类,然后创建了一种与该布局可能与之通信的类进行通信的方法,但是我可以对所有事件使用一个类。 Like if I make a game can 1 class handle the Main Menu layout from which by pressing buttons you will be able to go to Options, High Scores and New Game. 就像我制作一个游戏可以让1个班级处理主菜单布局一样,通过按下按钮,您可以进入选项,高分和新游戏。 Can I do everything on a class ? 我可以上课吗?
It's not a normal class that handles the layout. 它不是处理布局的普通类。 In Android, a class when extends an Activity or Fragment, it becomes one of the component of Android. 在Android中,当类扩展Activity或Fragment时,它成为Android的组件之一。
An Activity can render multiple layouts using setContentView()
method. 一个活动可以使用setContentView()
方法呈现多个布局。 You can call this method with different layout as you click on different buttons. 单击不同的按钮时,可以使用不同的布局调用此方法。
BUT , this is a bad approach. 但是 ,这是一个不好的方法。 To keep the same activity and change layouts, use Fragments . 要保持相同的活动并更改布局,请使用Fragments 。 Fragments are part of an Activity that can have their own life cycle and can be attached or detached from Activity dynamically. 片段是活动的一部分,可以有自己的生命周期,并且可以动态地与活动连接或分离。 They are mostly used for layouts for multiple sized devices. 它们主要用于多种尺寸设备的布局。 Read more about fragments here . 在此处阅读有关片段的更多信息。 Here is tutorial of how to use Fragment in Android: https://www.raywenderlich.com/117838/introduction-to-android-fragments-tutorial 这是有关如何在Android中使用Fragment的教程: https : //www.raywenderlich.com/117838/introduction-to-android-fragments-tutorial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.