activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t1"
android:textSize="24sp"/>
</LinearLayout>
MainActivity.java
package com.example.yanhsama.ex3_2;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.TextView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// 调用父类方法来加入系统菜单
super.onCreateOptionsMenu(menu);
// 添加菜单项
menu.add(
1, //组号
1, //唯一的ID号
1, //排序号
"菜单项1"); //标题
menu.add( 1, 2, 2, "菜单项2");
menu.add( 1, 3, 3, "菜单项3");
menu.add( 1, 4, 4, "菜单项4");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// String title = "选择了" + item.getTitle().toString();
switch (item.getItemId())
{ //响应每个菜单项(通过菜单项的ID)
case 1:
setContentView(R.layout.one);
// txt.setText(title);
break;
case 2:
setContentView(R.layout.two);
//txt.setText(title);
break;
case 3:
setContentView(R.layout.three);
// txt.setText(title);
break;
case 4:
setContentView(R.layout.four);
//txt.setText(title);
break;
default:
//对没有处理的事件,交给父类来处理
return super.onOptionsItemSelected(item);
}
return true;
}
}
one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt1"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt11"
android:textSize="30sp"/>
</LinearLayout>
two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt2"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt22"
android:textSize="30sp"/>
</LinearLayout>
three.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt3"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt33"
android:textSize="30sp"/>
</LinearLayout>
four.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt4"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt44"
android:textSize="30sp"/>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">ex3_2</string>
<string name="t1">菜单</string>
<string name="txt1">这是菜单项1</string>
<string name="txt11">欢迎来到古诗词界面!</string>
<string name="txt2">这是菜单项2</string>
<string name="txt22">欢迎来到游戏界面!</string>
<string name="txt3">这是菜单项3</string>
<string name="txt33">欢迎来到程序人生!</string>
<string name="txt4">这是菜单项4</string>
<string name="txt44">欢迎来到生活小贴士!</string>
</resources>
效果显示