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