Android--界面菜单

版权声明:孜孜不辍 https://blog.csdn.net/calculate23/article/details/85063421

前言

Android提供三种类型的菜单,分别是:选项菜单,子菜单,上下文菜单。前两种菜单类似于PC上的同类菜单那样,而上下文菜单是Android特有的,能够长按界面弹出的菜单,所以我们称之为上下文菜单。


使用方法

1,选项菜单
使用onCreateOptionsMenu方法设置布局的选项菜单,这个菜单你要在xml中写

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;
    }

之后在onOptionsItemSelected方法中设置单击了菜单要执行的事件,其中要返回一个boolearn类型,返回true的话活动会继续执行其他事件,相当于本事件结束。

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_clear) {
        	tv_answer.setText("");
        	et_divisor.setText("");
        	et_afterdivisor.setText("");
        }else if(id == R.id.action_quit){
        	finish(); 
        }
        return true;
    }

2,上下文菜单和选项菜单类似,只不过调用的方法不同,是onCreateContextMenu和onContextItemSelected。同时要在xml中写相应的menu布局。

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	// TODO Auto-generated method stub
    	getMenuInflater().inflate(R.menu.context, menu);
    	menu.setHeaderTitle("设置字体大小");
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	int id = item.getItemId();
        switch (id) {
		case R.id.Max:
			tv_content.setTextSize(27);
			break;
		case R.id.Min:
			tv_content.setTextSize(13);
			break;
		case R.id.Mid:
			tv_content.setTextSize(20);
			break;
		default:
			break;
		}
        return true;  //完成当前操作,继续其他任务
    }

3,对于子菜单可以在xml中进行嵌套

<item
        android:id="@+id/background"
        android:title="设置背景色">
    	<menu>
    	    <item
    	        android:id="@+id/white"
    	        android:title="白色"/>
    	    <item
    	        android:id="@+id/black"
    	        android:title="黑色"/>
    	    <item
    	        android:id="@+id/gray"
    	        android:title="灰色"/>
    	</menu>
    </item>

Android代码:

package cn.zhuangzhihuang.myreader;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.FrameLayout;
import android.widget.TextView;


public class MainActivity extends Activity {

	private FrameLayout sc_background;
	private TextView tv_content;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        sc_background = (FrameLayout) findViewById(R.id.sc_background);
        tv_content = (TextView) findViewById(R.id.tv_content);
        
        registerForContextMenu(tv_content);
    }


    @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) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
		case R.id.exit:
			finish(); //退出当前程序
			break;
		case R.id.black:
			sc_background.setBackgroundColor(Color.BLACK);
			tv_content.setTextColor(Color.WHITE);
			break;
		case R.id.white:
			sc_background.setBackgroundColor(Color.WHITE);
			tv_content.setTextColor(Color.BLACK);
			break;
		case R.id.gray:
			sc_background.setBackgroundColor(Color.GRAY);
			tv_content.setTextColor(Color.RED);
			break;
		default:
			break;
		}
        return true;  //完成当前操作,继续其他任务
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	// TODO Auto-generated method stub
    	getMenuInflater().inflate(R.menu.context, menu);
    	menu.setHeaderTitle("设置字体大小");
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	int id = item.getItemId();
        switch (id) {
		case R.id.Max:
			tv_content.setTextSize(27);
			break;
		case R.id.Min:
			tv_content.setTextSize(13);
			break;
		case R.id.Mid:
			tv_content.setTextSize(20);
			break;
		default:
			break;
		}
        return true;  //完成当前操作,继续其他任务
    }
    
}

选项菜单布局:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="cn.zhuangzhihuang.myreader.MainActivity" >

    <item
        android:id="@+id/background"
        android:title="设置背景色">
    	<menu>
    	    <item
    	        android:id="@+id/white"
    	        android:title="白色"/>
    	    <item
    	        android:id="@+id/black"
    	        android:title="黑色"/>
    	    <item
    	        android:id="@+id/gray"
    	        android:title="灰色"/>
    	</menu>
    </item>
    <item
        android:id="@+id/exit"
        android:title="退出"/>

</menu>

上下文菜单布局:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/Max" android:layout_width="wrap_content" android:layout_height="wrap_content" android:title="大号字"></item>
    <item android:id="@+id/Mid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:title="中号字"></item>
    <item android:id="@+id/Min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:title="小号字"></item>
    

</menu>

效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/calculate23/article/details/85063421