第一步:建一个资源文件 menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_1"
android:title="@string/menu_1" />
<item android:id="@+id/menu_2"
android:title="@string/menu_3" />
</menu>
第二步 写代码
onCreate事件代码
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationItemView naviItem = (BottomNavigationItemView)findViewById(R.id.navigation_dashboard) ;
mPopupMenu = new PopupMenu(this,naviItem);
mPopupMenu.inflate(R.menu.menu); //两种inflate方式都可以
// mPopupMenu.getMenuInflater().inflate(R.menu.navigation, mPopupMenu.getMenu());
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), "这个是PopupMenu的演示demo", Toast.LENGTH_LONG).show();
return true;
}
});
BottomNavigationView.OnNavigationItemSelectedListener事件
mPopupMenu.setGravity(Gravity.END); //设置显示的位置
mPopupMenu.show();