简体   繁体   English

android studio中的一个类可以处理几种布局和用途不同的XML布局吗

[英]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.

相关问题 Android:在一种布局中使用2种布局 - Android: Using 2 layouts in one layout 一个xml布局中的多个布局-单击按钮后不追加文本 - Several layouts in one xml layout - doesn't append text after button clicked 更新Android Studio 3.2后,布局文件夹未显示layouts.xml文件 - Layout folders not showing layouts.xml files after updating Android Studio 3.2 如何动态将布局和视图添加到Android XML布局文件? - How can I dynamically add layouts and views to an Android XML layout file? 使用带有android xml布局的GLSurfaceView类 - Using GLSurfaceView class with android xml layouts 如何在 Android Studio 中使用布局? - How can I work with layouts in Android Studio? “布局”是否与 Android Studio 中的“布局资源”相同? 如何以编程方式创建 XML 布局资源? - Is "layout" the same as "layout resource" in Android Studio? How can I programmingly create an XML layout resource? Android:将选择的不同布局动态加载到另一个布局中 - Android: dynamically loading different layouts of choice into another layout 具有类的Android布局XML文件 - Android Layout XML file with class XML,Android Studio中的“无法实例化一个或多个类”错误 - “Failed to instantiate one or more class” error in XML, Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM