在Android中,菜单的种类有很多种,其中大方向上很多书都是说分成三种,即是:选项菜单(Option Menu)、上下文菜单(Context Menu)、子菜单(Sub Menu),在分类这个上,我确实是记不住,不过个人感觉我们还要会使用它就可以啦。下面我们就讲解几种菜单咯!
一:选项菜单(Option Menu)
选项菜单,它主要有两种创建方式,一种是我们直接使用代码进行创建;第二种是我们使用xml方式来创建,当然它不仅仅是创建一个布局就可以解决的。选项菜单的话就是我们有些手机它在屏幕的下方会有菜单按钮,可以用来触发我们的菜单,又或者是直接在我们的APP右上方有一个系统的三个点的图标来触发的!
(1)通过代码的方式创建:
创建MyOptionMenuActivity.class
public class MyOptionMenuActivity extends AppCompatActivity {
public static final int first = 0;
public static final int second = 1;
public static final int third = 2;
public static final int forth = 3;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_menu);
}
/*groupId:菜单项所在的组
itemId:菜单项编号
order:排序
title:标题*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, first, 0, "添加");
menu.add(0, second, 1, "删除");
menu.add(0, third, 2, "喜欢");
menu.add(0, forth, 3, "不喜欢");
return true;
}
//给菜单选项设置监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case first:
Toast.makeText(MyOptionMenuActivity.this, "添加", Toast.LENGTH_SHORT).show();
break;
case second:
Toast.makeText(MyOptionMenuActivity.this, "删除", Toast.LENGTH_SHORT).show();
break;
case third:
Toast.makeText(MyOptionMenuActivity.this, "喜欢", Toast.LENGTH_SHORT).show();
break;
case forth:
Toast.makeText(MyOptionMenuActivity.this, "不喜欢", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
}
(2)通过代码来创建菜单:
新建layout_optionmenutwo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--表明使用了Option Menu-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Menu"
android:textSize="30sp"/>
</LinearLayout>
res/menu/optionmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--菜单项需要显示的内容-->
<item
android:id="@+id/music"
android:title="音乐"/>
<item
android:id="@+id/sport"
android:title="运动"/>
<item
android:id="@+id/read"
android:title="阅读"/>
</menu>
新建MyOptionMenuActivityTwo.class
public class MyOptionMenuActivityTwo extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_optionmenutwo);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//通过getMenuInflater()得到菜单实例
getMenuInflater().inflate(R.menu.optionmenu,menu);
return true;
}
}
二、上下文菜单(Context Menu)
上下文菜单,我们这里在ListView中进行模拟,使用ListActivity,数据就是简单的模拟一下。
新建layout_contextmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--使用ListActivity所以要用一个系统的list的ID-->
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:divider="#FF0000"/>
</LinearLayout>
新建MyContextMenuActivity.class
public class MyContextMenuActivity extends ListActivity {
private static final int first = 0;
private static final int second = 1;
private static final int third = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_contextmenu);
//模拟数据
List<String> datas = fullList();
//使用ArrayAdapter,布局使用系统自带的android.R.layout.simple_list_item_1
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyContextMenuActivity.this, android.R.layout.simple_list_item_1, datas);
//绑定适配器
setListAdapter(adapter);
getListView().setOnCreateContextMenuListener(this);
//设置监听
}
private List<String> fullList() {
List<String> datas = new ArrayList<String>();
for (int i = 1; i < 8; i++) {
datas.add("星期" + i);
}
return datas;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("要做的事");
menu.add(0,first,0,"听音乐");
menu.add(0,second,0,"打篮球");
menu.add(0,third,0,"看书");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case first:
Toast.makeText(MyContextMenuActivity.this,"听音乐",Toast.LENGTH_SHORT).show();
break;
case second:
Toast.makeText(MyContextMenuActivity.this,"打篮球",Toast.LENGTH_SHORT).show();
break;
case third:
Toast.makeText(MyContextMenuActivity.this,"看书",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
三、子菜单(Sub Menu)
子菜单指的是在我们设置的菜单上,再次设置的菜单,这里因为我是使用实体机调试的,我的手机是没有自带的menu键的,所以我就采用在标题栏触发菜单这种。
新建layout_submenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submenu使用"
android:textSize="50dp"/>
</LinearLayout>
新建MySubMenuActivity.class
public class MySubMenuActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_submenu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*groupId:菜单项所在的组
itemId:菜单项编号
order:排序
title:标题*/
menu.add(0, 1, 0, "增加");
menu.add(0, 2, 0, "删除");
//创建SubMenu,使用addSubMenu()创建主项,然后使用.add创建副项(子项菜单),并且设置好他们的ItemId,只要是为了在后面给他们设置子项的监听器。
SubMenu SM = menu.addSubMenu(0, 3, 0, "喜欢程度>>");
SM.add(3, 301, 1, "非常喜欢");
SM.add(3, 302, 2, "较喜欢");
SM.add(3, 303, 3, "喜欢");
SubMenu sm = menu.addSubMenu(0, 4, 0, "讨厌程度>>");
sm.add(4, 401, 1, "非常讨厌");
sm.add(4, 402, 2, "较讨厌");
sm.add(4, 403, 3, "讨厌");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(getApplicationContext(), "增加", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(getApplicationContext(), "删除", Toast.LENGTH_SHORT).show();
return true;
//在此处为子项设置监听器
case 301:
Toast.makeText(getApplicationContext(), "非常喜欢", Toast.LENGTH_SHORT).show();
return true;
case 302:
Toast.makeText(getApplicationContext(), "较喜欢", Toast.LENGTH_SHORT).show();
return true;
case 303:
Toast.makeText(getApplicationContext(), "喜欢", Toast.LENGTH_SHORT).show();
return true;
case 401:
Toast.makeText(getApplicationContext(), "非常讨厌", Toast.LENGTH_SHORT).show();
return true;
case 402:
Toast.makeText(getApplicationContext(), "较讨厌", Toast.LENGTH_SHORT).show();
return true;
case 403:
Toast.makeText(getApplicationContext(), "讨厌", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
四、弹出式菜单(Popup Menu)
此类菜单只要是绑定在某一个View上面的,一般显示在绑定View 的下方(如果下方有空间可以显示的话)
新建layout_popupmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--设置了一个按钮主要是为了来触发Popup Menu-->
<Button
android:id="@+id/popupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="PopupMenu"
android:textAllCaps="false"/>
</LinearLayout>
新建MyPopupMenuActivity.class
public class MyPopupMenuActivity extends AppCompatActivity implements View.OnClickListener,PopupMenu.OnMenuItemClickListener{
//注意这里是需要实现两个接口的,一个是按钮的本身监听事件View.OnClickListener,另外一个是PopupMenu.OnMenuItemClickListener,这一个是为了实现我们的菜单内容项的监听事件的。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_popupmenu);
Button popupBtn = (Button) findViewById(R.id.popupBtn);
popupBtn.setOnClickListener(this);
}
//为按钮设置监听
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(this,v);
//找到布局实例菜单
popup.getMenuInflater().inflate(R.menu.popupmenu,popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
//为菜单项设置监听
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.music:
Toast.makeText(this, "我爱音乐", Toast.LENGTH_SHORT).show();
break;
case R.id.sport:
Toast.makeText(this, "我爱运动", Toast.LENGTH_SHORT).show();
break;
case R.id.read:
Toast.makeText(this, "我爱看书", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
}
对几种基本的菜单介绍就到这里了,当然菜单类型远远还不止这几种,还有选择菜单(Alertnative Menu),拓展菜单,图标菜单,浮动菜单,浮动选择菜单等等。我们学习完了基本的,其他拓展的就可以进行尝试。