
[英]How to make a 2 buttons that show/hide different layouts? (android, eclipse)
[英]How to make android buttons switch different XML layouts?
我最近才刚开始进行一些android编程,并且正在创建一个小型项目应用程序。 我有几个不同的XML布局文件,希望我的按钮转到该布局。 但是,当我按模拟器上的按钮时,我收到此消息“不幸的是,“ MyApp”已停止”。
这是我的主要活动课:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Shows the Activities screen.
Button Button1=(Button)findViewById(R.id.Button1);
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,ActivityList.class);
startActivity(i);
}
});
}
这是我的按钮的XML代码:
android:id="@+id/Button1"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_alignLeft="@+id/Menutxt"
android:layout_below="@+id/Menutxt"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/Choose_Activity"
android:textColor="#ffffffff"
android:textSize="25sp"
android:typeface="serif"
我想知道是否有人可以在这里帮助我,尝试了几个小时却无法超越。 谢谢。
将此添加到您要打开的活动的布局中。 它应该放在顶部: tools:context=".activity" >
例如<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".activity" >
用您的活动名称替换“活动”
您没有向我们展示您的ActivityList.class,也许您已经创建并扩展了Activity,但是请确保在AndroidManifest.xml中具有以下代码:
活动android:name =“。ActivityList”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.