首页 > 代码库 > Android Meun 用法

Android Meun 用法

Android Meun 用法

点击菜单实体键弹出菜单:如下图

技术分享

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MenuTest"/>
</LinearLayout>

item_menu.xml(要在rec文件夹下建一个menu文件夹,然后在menu文件夹下再建此文件)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/main_menu_1" android:title="新增"></item>
    <item android:id="@+id/main_menu_2" android:title="修改"></item>
    <item android:id="@+id/main_menu_3" android:title="删除"></item>
</menu>

MainActivity.java

package com.zy.viewpager;

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

/**
 * Created by Administrator on 2016/11/19.
 */

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_view);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.item_menu,menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()){
            case R.id.main_menu_1:
                Toast.makeText(this,"用户点击了" + item.getTitle(),Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_menu_2:
                Toast.makeText(this,"用户点击了" + item.getTitle(),Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_menu_3:
                Toast.makeText(this,"用户点击了" + item.getTitle(),Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this,"TEST",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onMenuItemSelected(featureId,item);
    }
}

测试效果:

技术分享

 

技术分享

 结束!!

 

Android Meun 用法