Android创建菜单栏(android布局实现)

Android创建菜单栏(android布局实现)
res/menu里的main.xml布局文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.Menudemo1.MainActivity" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="设置">
    </item>
    <item
        android:id="@+id/action_about"
        android:orderInCategory="101"
        android:showAsAction="never"
        android:title="关于">
    </item>
    <item
        android:id="@+id/action_add"
        android:orderInCategory="102"
        android:showAsAction="never"
        android:title="增加">
    </item>
    <item
        android:id="@+id/action_save"
        android:orderInCategory="103"
        android:showAsAction="never"
        android:title="保存">
    </item>

</menu>

代码如下:
package com.example.Menudemo1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

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

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
	return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();

		return super.onOptionsItemSelected(item);
	}
}

运行结果:



你可能感兴趣的:(android)