MenuInFlater的用法

MenuInfalterTest.java
public class MenuInflaterTest extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

    }

 

    public boolean onCreateOptionsMenu(Menu menu) {

       // 获取当前的菜单

       MenuInflater inflater = getMenuInflater();

       // 填充菜单

       inflater.inflate(R.menu.option_menu, menu);

       return true;

    }

 

    /**

     * 对菜单点击事件处理

     */

    public boolean onOptionsItemSelected(MenuItem item) {

       switch (item.getItemId()) {

       case R.id.menu_add:

           break;

       case R.id.menu_wallaper:

           break;

       case R.id.menu_search:

           break;

       case R.id.menu_setting:

           showSettings();

           break;

       }

       return super.onOptionsItemSelected(item);

    }

 

    /**

     * 显示设置选项

     */

    private void showSettings() {

       Intent settings = new Intent

       (android.provider.Settings.ACTION_SETTINGS);

       settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 

              | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

       startActivity(settings);

    }

}

}


Main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    />

</LinearLayout>


Option_menu.xml
<?xml version="1.0" encoding="utf-8"?>  

<menu xmlns:android="http://schemas.android.com/apk/res/android">  

    <item android:id="@+id/menu_add"  

        android:title="Add"  

        android:icon="@android:drawable/ic_menu_add"/>  

     <item android:id="@+id/menu_wallaper"  

        android:title="Wallpaper"  

        android:icon="@android:drawable/ic_menu_gallery"/>  

    <item  android:id="@+id/menu_search"  

        android:title="Search"  

        android:icon="@android:drawable/ic_search_category_default"/>  

    <item  android:id="@+id/menu_setting"  

        android:title="Settings"  

      android:icon="@android:drawable/ic_menu_preferences"/>                             </menu>  

你可能感兴趣的:(menuInflater)