简体   繁体   English

如何为不同的活动使用相同的 XML 布局?

[英]How can I use the same XML layout for different activities?

I am making a quiz app which includes 50 different quizzes.我正在制作一个测验应用程序,其中包括 50 个不同的测验。 I have a basic quiz layout with buttons and text views, and I want to use this layout with all my activities.我有一个带有按钮和文本视图的基本测验布局,我想在我的所有活动中使用这个布局。 For example, if the player choose the first quiz, the layout should come automatically and my Java code should provide the corresponding answers/questions.例如,如果玩家选择第一个测验,布局应自动出现,我的 Java 代码应提供相应的答案/问题。 How can I do this?我怎样才能做到这一点?

Put layout id in setContentView(layout id) in your activity's onCreate() method.将布局 id 放在您活动的onCreate()方法中的setContentView(layout id)中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
}

in your other activities just changue your setContentView id like this :-在您的其他活动中,只需像这样更改您的 setContentView id:-

old contentViewId:-旧的 contentViewId:-

setContentView(R.layout.yourCurrentView);

change id:-更改 ID:-

setContentView(R.layout.yourNewView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的活动上使用GridView的同一适配器 - How Can I use Same Adapter of a GridView on Different activities 我可以在不同的活动中为两个不同的列表视图使用相同的适配器吗? - Can I use the same adapter for two different listviews in different activities? 如何在android中使用相同的XML布局创建多个活动,每个活动都有自己的自定义视图 - How to use same XML layout in android to create several activities, each with their own custom view 我可以在两个活动中使用相同的方法吗? - Can i use the same method in two activities? 我怎样才能同时运行2个活动 - How can i run 2 activities at the same time “布局”是否与 Android Studio 中的“布局资源”相同? 如何以编程方式创建 XML 布局资源? - Is "layout" the same as "layout resource" in Android Studio? How can I programmingly create an XML layout resource? 如何在不同的活动(Android和Java)中使用相同的ArrayList - How to use the same ArrayList in different activities (Android and Java) 如何在不复制代码的情况下在不同的活动上做同样的事情 - how can make the same thing on different activities without copying code 如何在不使用活动的情况下创建其他页面 - How can i create different pages without using activities 如何在同一pom.xml中使用selendroid和seleniumhq? - How can I use selendroid and seleniumhq in same pom.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM