Menu(选项菜单,上下文菜单,弹出式菜单)

选择菜单,两种方式:一个是手动创建添加,另一个时通过布局填充器添加

public class MyMenu extends Activity {

    //Menu会显示在左上角
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    // 用于创建菜单选项菜单,在打开界面时会自动调用
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //手动创建
        // 组的ID,当前选项的ID,排序,标题名称
        /*menu.add(0, 100, 1, "导航");
        menu.add(0, 200, 2, "功能");
        menu.add(0, 300, 3, "设置");*/
        //菜单填充器
        getMenuInflater().inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }

    // 通过item来确定选项菜单的单击时间
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        //手动创建
        /*switch (itemId) {
        case 100:
            Toast.makeText(this, "点击的是导航", Toast.LENGTH_LONG).show();
            break;
        case 200:
            Toast.makeText(this, "功能", Toast.LENGTH_LONG).show();
            break;
        case 300:
            Toast.makeText(this, "设置", Toast.LENGTH_LONG).show();
            break;
        }*/
        //填充器
        switch (itemId) {
        case R.id.one:
            Toast.makeText(this, "点击的是导航", Toast.LENGTH_LONG).show();
            break;
        case R.id.tow:
            Toast.makeText(this, "功能", Toast.LENGTH_LONG).show();
            break;
        case R.id.there:
            Toast.makeText(this, "设置", Toast.LENGTH_LONG).show();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

}

上下文菜单,需要注册上下文菜单

public class MycontectMenuActivity extends Activity {

    private TextView tv;
    //上下文菜单
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycontect_menu);
        tv=(TextView) findViewById(R.id.ontouch);
        //注册上下文菜单到组件上    基于长按点击事件
        registerForContextMenu(tv);
    }

    //创建上下文菜单的事件方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //menu.add(arg0);
        getMenuInflater().inflate(R.menu.mycontect_menu, menu);
    }

    //上下文单击时间
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
        case R.id.red:
            tv.setBackgroundColor(Color.RED);
            break;
        case R.id.bolue:
            tv.setBackgroundColor(Color.BLUE);
            break;
        case R.id.green:
            tv.setBackgroundColor(Color.GREEN);
            break;
        }
        return super.onContextItemSelected(item);
    }

}

弹出式菜单

public class MycontectMenuActivity extends Activity {

    private TextView tv;
    //上下文菜单
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycontect_menu);
        tv=(TextView) findViewById(R.id.ontouch);
        //注册上下文菜单到组件上    基于长按点击事件
        registerForContextMenu(tv);
    }

    //创建上下文菜单的事件方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //menu.add(arg0);
        getMenuInflater().inflate(R.menu.mycontect_menu, menu);
    }

    //上下文单击时间
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
        case R.id.red:
            tv.setBackgroundColor(Color.RED);
            break;
        case R.id.bolue:
            tv.setBackgroundColor(Color.BLUE);
            break;
        case R.id.green:
            tv.setBackgroundColor(Color.GREEN);
            break;
        }
        return super.onContextItemSelected(item);
    }

}

Menu文件下的布局,可以添加子菜单

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

    
    <item
        android:id="@+id/s"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="小号"/>
    <item
        android:id="@+id/xl"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="中号"/>
    <item
        android:id="@+id/xxl"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="大号"/>
    <item
        android:id="@+id/xxxl"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="特号">
        <menu>
            <item
                android:id="@+id/xxxxl"
                android:orderInCategory="100"
                android:showAsAction="never"
                android:title="特二"/>
        menu>
    item>

menu>

你可能感兴趣的:(UI)