1.选项菜单
另外有一点,在选项菜单中,每个菜单选项的图标是不能在actionBar外面显示出来的,虽然网上有别的方法,但是我试不成功,只有上下文菜单的时候可以显示图标
第一种:在menu资源中添加菜单
关于showAsAction的几种常用属性
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item1"
android:title="菜单"
android:orderInCategory="1"
android:icon="@android:drawable/ic_delete"
app:showAsAction="never">
item>
<item
android:id="@+id/item2"
android:title="菜单"
android:orderInCategory="2"
android:icon="@android:drawable/ic_delete"
app:showAsAction="never">
item>
<item
android:id="@+id/item3"
android:title="菜单"
android:orderInCategory="3"
android:icon="@android:drawable/ic_delete"
app:showAsAction="never">
item>
menu>
activity:
package com.example.john.test1;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//创建选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//菜单填充
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//菜单项的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.item1:
Toast.makeText(this, "菜单1", Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(this, "菜单2", Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(this, "菜单3", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
第二种:在代码中添加菜单
package com.example.john.test1;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//创建选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//groupId,itemId,序号,名称
menu.add(0,100,1,"菜单1").setIcon(android.R.drawable.bottom_bar);
menu.add(0,200,2,"菜单2").setIcon(android.R.drawable.bottom_bar);
menu.add(0,300,3,"菜单3").setIcon(android.R.drawable.bottom_bar);
return super.onCreateOptionsMenu(menu);
}
//菜单项的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case 100:
Toast.makeText(this, "菜单1", Toast.LENGTH_LONG).show();
break;
case 200:
Toast.makeText(this, "菜单2", Toast.LENGTH_LONG).show();
break;
case 300:
Toast.makeText(this, "菜单3", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
2.上下文菜单
上下文菜单是长按某个控件触发的,所以要用到下面这个方法。
registerForContextMenu(tv_id);
关于在代码中添加菜单和在menu中添加菜单同选项菜单是一样的,只不过在menu中添加菜单的时候不需要设置showAsAction,因为不是显示在actionBar了
package com.example.john.test1;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.lang.reflect.Method;
public class Main2Activity extends AppCompatActivity {
private TextView tv_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv_id = (TextView)findViewById(R.id.id);
//给text注册上下文菜单
registerForContextMenu(tv_id);
}
//创建上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
setIconEnable(menu,true); //设置图标可以显示
//设置上下文菜单的头图标
menu.setHeaderIcon(android.R.drawable.ic_delete);
//设置上下文菜单的头文字
menu.setHeaderTitle("改变文字背景颜色");
//填充菜单
getMenuInflater().inflate(R.menu.main2_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//上下文菜单的点击事件
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
tv_id.setBackgroundColor(Color.WHITE);
break;
case R.id.item2:
tv_id.setBackgroundColor(Color.BLUE);
break;
case R.id.item3:
tv_id.setBackgroundColor(Color.GRAY);
break;
}
return super.onContextItemSelected(item);
}
//我试的时候这个方法只有上下文菜单可以,具体我也不是很懂
private void setIconEnable(Menu menu, boolean enable)
{
try
{
Class> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
m.setAccessible(true);
//MenuBuilder实现Menu接口,创建菜单时,传进来的menu其实就是MenuBuilder对象(java的多态特征)
m.invoke(menu, enable);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
3.弹出式菜单
弹出式菜单是点击某个控件,在其下方显示的菜单
弹出式菜单和其他两种菜单的构建方式稍微有一点不同,就是要自己创建PopupMenu
不过添加菜单选项的方式是一样的,都分为代码中添加和menu资源中添加,这里就展示代码中如何添加
package com.example.john.test1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.lang.reflect.Method;
public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//获得button
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
//单击事件,点击按钮弹出菜单
@Override
public void onClick(View v) {
//创建弹出式菜单,在指定的view下弹出
PopupMenu popupMenu = new PopupMenu(this,v);
//填充菜单
popupMenu.getMenuInflater().inflate(R.menu.main3_menu,popupMenu.getMenu());
//监听事件
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//弹出式菜单的选项的单击事件
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(Main3Activity.this,"item1",Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(Main3Activity.this,"item2",Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(Main3Activity.this,"item3",Toast.LENGTH_LONG).show();
break;
}
return false;
}
});
//显示弹出式菜单
popupMenu.show();
}
}
还有一个就是关于子菜单,上面的三种菜单都可以添加子菜单
子菜单也有两种添加方式,分为代码中添加和menu资源中添加
第一种:在menu资源中
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="item1"
android:orderInCategory="1"
android:id="@+id/item1">
<menu>
<item
android:title="item1_1"
android:orderInCategory="1"
android:id="@+id/item1_1">
item>
<item
android:title="item1_2"
android:orderInCategory="1"
android:id="@+id/item1_2">
item>
<item
android:title="item1_3"
android:orderInCategory="1"
android:id="@+id/item1_3">
item>
menu>
item>
<item
android:title="item2"
android:orderInCategory="1"
android:id="@+id/item2">
<menu>
<item
android:title="item2_1"
android:orderInCategory="1"
android:id="@+id/item2_1">
item>
<item
android:title="item2_2"
android:orderInCategory="1"
android:id="@+id/item2_2">
item>
<item
android:title="item3_3"
android:orderInCategory="1"
android:id="@+id/item3_3">
item>
menu>
item>
menu>
第二种:在代码中添加
以弹出式菜单为例。其他两种都是一样的
package com.example.john.test1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.lang.reflect.Method;
public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//获得button
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
//单击事件
@Override
public void onClick(View v) {
//创建弹出式菜单,在指定的view下弹出
PopupMenu popupMenu = new PopupMenu(this,v);
//添加子菜单add(groupId,itemId,序号,title)
SubMenu subMenu = popupMenu.getMenu().addSubMenu(0,100,1,"菜单1");
subMenu.add(0,100,1,"子菜单1_1");
subMenu.add(0,200,2,"子菜单1_2");
SubMenu subMenu2 = popupMenu.getMenu().addSubMenu(0,200,2,"菜单2");
subMenu2.add(0,100,1,"子菜单2_1");
subMenu2.add(0,200,2,"子菜单2_2");
//监听事件
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//弹出式菜单的选项的单击事件
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(Main3Activity.this,"item1",Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(Main3Activity.this,"item2",Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(Main3Activity.this,"item3",Toast.LENGTH_LONG).show();
break;
}
return false;
}
});
//显示弹出式菜单
popupMenu.show();
}
}