Android Studio之菜单栏制作
静态菜单栏XML文件准备工作
一、准备好布局首页展示页面XML文件
该布局文件适应于三种菜单栏。
此处说明一下菜单栏的种类:选项菜单(也称三点菜单键)、上下文菜单(长按菜单)、弹出式菜单(点击菜单)
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="山有木兮木有枝,心悦君兮君不知"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.242" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项一"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.69"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.254" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项二"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.309"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.252" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、准备好菜单栏的布局文件
1、先建一个存放菜单栏的XML文件夹,看下图的创建步骤,先找到res->New->Directory
2、填写文件夹的名字,最好看名知意。(menu)
3、创建一个menu的资源文件,menu -> New -> Menu Resource File
4、填写新建的资源文件的名字
这里我创建了两个菜单资源文件:
代码如下:
menu1:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:id="@+id/one1"
android:title="菜单一"></item>
<item android:id="@+id/one2"
android:title="菜单二"></item>
<item android:id="@+id/one3"
android:title="菜单三"></item>
<item android:id="@+id/one4"
android:title="菜单四"></item>
</menu>
menu2:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent">
<item android:id="@+id/bu1"
android:title="选项一"></item>
<item android:id="@+id/bu2"
android:title="选项一"></item>
<item android:id="@+id/bu3"
android:title="选项一"></item>
</menu>
准备好之后,下面开始写Java代码。
选项菜单
1、定义所需要的全局变量
2、在函数onCreate(Bundle savedInstanceState)函数里获取上面定义的全局变量的资源
上面两步是必写的,下面的步骤不介绍了。
3、重写Activity的onCreateOptionsMenu(Menu menu)方法,在该方法中调用Menu对象来添加菜单或者子菜单。
4、需要应用程序响应菜单项的点击事件,可重写Activity的onOptionsItemSelected(MenuItem item)方法,调用item.getItemId()获得被点击菜单项的ID,做出不同的响应
java代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//定义按钮的全局变量
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 绑定按钮的资源(利用id找到xml文件中的按钮)
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
}
@Override//重写获取菜单项的方法
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();//获取菜单Inflater,MenuInflater加载menu布局文件
inflater.inflate(R.menu.menu1,menu);//通过inflate获取菜单资源
return true;
}
@Override//菜单项被点击时的逻辑处理
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.one1:
Toast.makeText(MainActivity.this,"我是一",Toast.LENGTH_LONG).show();
break;
case R.id.one2:
Toast.makeText(MainActivity.this,"我是二",Toast.LENGTH_LONG).show();
break;
case R.id.one3:
Toast.makeText(MainActivity.this,"我是三",Toast.LENGTH_LONG).show();
break;
case R.id.one4:
Toast.makeText(MainActivity.this,"我是四",Toast.LENGTH_LONG).show();
break;
default:
return true;
}
return true;
}
}
效果图如下:
上下文菜单
1、重写Activity的onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)方法。
2、调用Activity的registerForContextMenu(textView)方法为View组件注册上下文菜单
2、重写Activity的onContextItemSelected(MenuItem item)响应菜单项的选择操作,调用item.getItemId()获得被点击菜单项的ID,做出不同的响应
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//定义按钮的全局变量
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 绑定按钮的资源(利用id找到xml文件中的按钮)
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
//为组件注册上下文菜单
registerForContextMenu(textView);
}
//重写Activity的onCreateContextMenu()
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
switch (v.getId()){
case R.id.textView:
menu.setHeaderTitle("分享到,,,");
menu.add(0,200,0,"qq空间");
menu.add(0,201,0,"朋友圈");
SubMenu submenu = menu.addSubMenu(0,200,0,"微博");
submenu.add(0,202,0,"新浪");
submenu.add(0,203,0,"新浪");
submenu.add(0,204,0,"新浪");
default:
break;
}
super.onCreateContextMenu(menu,v,menuInfo);
}
//重写Activity的onContextItemSelected()
public boolean onContextItemSelected(MenuItem item){
switch (item.getItemId()){
case 200:
Toast.makeText(MainActivity.this,"你选了一",Toast.LENGTH_LONG).show();
break;
case 201:
Toast.makeText(MainActivity.this,"你选了二",Toast.LENGTH_LONG).show();
break;
case 203:
Toast.makeText(MainActivity.this,"你选了三",Toast.LENGTH_LONG).show();
break;
case 204:
Toast.makeText(MainActivity.this,"你选了四",Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
效果图如下:(长按哦!)
弹出式菜单
1、调用new PopupMenu(Context context,View anchor)创建下拉菜单,anchor代表要激发该弹出菜单的组件
2、调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中
turn super.onOptionsItemSelected(item);
}
return true;
}}
弹出式菜单
1、调用new PopupMenu(Context context,View anchor)创建下拉菜单,anchor代表要激发该弹出菜单的组件
2、调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中
3、调用PopupMenu的show()方法显示弹出式菜单
package com.example.myapplication;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuPopupHelper;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
//定义按钮的全局变量
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 绑定按钮的资源(利用id找到xml文件中的按钮)
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
registerForContextMenu(textView);
button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("RestrictedApi")
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
//创建PopupMenu
PopupMenu popupMenu = new PopupMenu(MainActivity.this,button);
//加载菜单资源
popupMenu.getMenuInflater().inflate(R.menu.menu2, popupMenu.getMenu());
//菜单事件监听
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.bu1:
Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_LONG).show();
break;
case R.id.bu2:
Toast.makeText(MainActivity.this,"粘贴",Toast.LENGTH_LONG).show();
break;
case R.id.bu3:
Toast.makeText(MainActivity.this,"清除",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
//使用反射,强制显示菜单图标
try {
Field field = popupMenu.getClass().getDeclaredField("mm");
field.setAccessible(true);
MenuPopupHelper menuPopupHelper = (MenuPopupHelper) field.get(popupMenu);
menuPopupHelper.setForceShowIcon(true);
}catch (IllegalAccessException | NoSuchFieldException e){
e.printStackTrace();
}
popupMenu.show();
}
});
}
}
///
///
///
关于Java代码有很多包名,如果有哪个方法没有找到什么包可以 “Alt+Enter”快捷键帮你找到这个地方的可能需要要导入的包或者其他。
整个代码的引入是从第一个写到最后一个菜单的累加的。