Android多视图切换,无活动';s
我正在制作一个游戏,我正在制作一个具有3种不同视图(监狱、院子和沙子)的LevelSelect类,我以前有多个意图,但如果我这样做,就没有好方法关闭我的应用程序,而且它也不会很好地返回 我还尝试使用Android多视图切换,无活动';s,android,multiple-views,Android,Multiple Views,我正在制作一个游戏,我正在制作一个具有3种不同视图(监狱、院子和沙子)的LevelSelect类,我以前有多个意图,但如果我这样做,就没有好方法关闭我的应用程序,而且它也不会很好地返回 我还尝试使用setContentView()方法,但结果是它无法加载具有视图的类,然后我尝试组合该类,但该类无法初始化进程中当前视图之外的对象。 所以我的问题是: 如何在不启动新活动的情况下使用多个视图 package com.muttgames.shadowpuzzle; import android.app
setContentView()
方法,但结果是它无法加载具有视图的类,然后我尝试组合该类,但该类无法初始化进程中当前视图之外的对象。
所以我的问题是:
如何在不启动新活动的情况下使用多个视图
package com.muttgames.shadowpuzzle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class LevelSelect extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.jail_level_view);
//Defining Inflater
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Defining sandView layout
RelativeLayout jailView = (RelativeLayout) findViewById(R.id.jail_level);
View innerViewsJailView = inflater.inflate(R.id.yard_level, jailView, true);
View jailMenu = innerViewsJailView.findViewById(R.id.jail2menu);
jailMenu.setOnClickListener(this);
//Defining sandView layout
RelativeLayout yardView = (RelativeLayout) findViewById(R.id.yard_level);
View innerViewsYardView = inflater.inflate(R.id.yard_level, yardView, true);
View yardMenu = innerViewsYardView.findViewById(R.id.yard2menu);
yardMenu.setOnClickListener(this);
//Defining sandView layout
RelativeLayout sandView = (RelativeLayout) findViewById(R.id.sand_level);
View innerViewsSandView = inflater.inflate(R.id.sand_level, sandView, true);
View sandMenu = innerViewsSandView.findViewById(R.id.sand2menu);
sandMenu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.jail2menu:
finish();
break;
case R.id.yard2menu:
finish();
break;
case R.id.sand2menu:
finish();
break;
}
}
}
我确实意识到这是一个非常小的类,但我做了一些小的试用,以避免自己不得不一次又一次地编写大量的代码
这部分中的错误是我初始化了yardMenu和sandMenu按钮,而它们不在R.layout.jail\u level\u视图
xml文件中
此代码用于在有人想要返回到主\u活动时停止levelselect活动
我怎样才能改变它呢?我可以把它改成activity\u main
xml文件,而不是关闭活动
xml文件(它们看起来几乎都一样,颜色稍有调整等等):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/jail_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Jail Levels"
android:textSize="30sp" />
<Button
android:id="@+id/level1"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="45dp"
android:layout_marginTop="30dp"
android:background="@drawable/jail_button"
android:text="1" />
<Button
android:id="@+id/level2"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/level1"
android:background="@drawable/jail_button"
android:text="2" />
<Button
android:id="@+id/level3"
style="@drawable/jail_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/view_title"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/level2"
android:background="@drawable/jail_button"
android:text="3" />
<Button
style="@drawable/yard_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="225dp"
android:background="@drawable/yard_button"
android:text="Next" />
<Button
android:id="@+id/jail2menu"
style="@drawable/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="122dp"
android:background="@drawable/menu_button"
android:text="Menu" />
</RelativeLayout>
您需要一个布局充气机和一个容器。
例如,将“sandMenu”设置为线性布局,然后设置为代码:
sandMenu.removeAllViews();
LayoutInflater inflater = (LayoutInflater) .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View innerViewsSandMenu = inflater.inflate(R.layout.yourLAYOUT, sandMenu, true);
稍后,您可以通过以下方式访问sandMenu的内部视图:
innerViewsSandMenu.findViewById(R.id.....
我用sandMenu
来理解它,你是说我的sand_level_视图
,我的sandMenu
实际上是一个按钮,通过使用finish()从我的sand_level_视图
更改为我的活动
不管怎样,我应该把这部分放在哪里,如何初始化sand_level_视图
,这样我就可以在你刚刚给我的代码中使用它了?很抱歉,我问了这么多,我大约一周前才开始。我附加到你的代码是为了在这种情况下“监狱level_视图”中放大布局对于容器,当您将此布局充气后,您可以使用“水平视图”中的视图通过FindViewByi,我认为我得到了它的功能,它初始化了,或者在android中;将“监狱级别”视图膨胀为一个对象,您可以稍后从中调用变量。无论如何,它不起作用,它仍然不会启动我的LevelSelect.class
,当我单击尝试从我的MainActivity.class
打开它时,您可以编辑我的代码以便它可以工作?:/在onCreate中,将膨胀线放在setContentView后面,并小心使用sand_level_view,因为它的宽度、高度和方向正确,您也可以共享XML吗?好的,我让它们工作了,只有一个点还没有工作。我声明了我的3个视图(jail_level_view yard_level_view sand_level_view
)但是,当我试图声明我的按钮时,它不再起作用了……我将用我目前拥有的代码更新我的主要帖子。
innerViewsSandMenu.findViewById(R.id.....