LiangGaoUser / AndroidEbook

0 stars 0 forks source link

利用 toolbar来优化书架页面 #7

Open LiangGaoUser opened 4 years ago

LiangGaoUser commented 4 years ago

@https://www.jianshu.com/p/ddf7f67c3e8f 注意里面有类似Popupmenu的功能 关于popupmenu

private void showPopupMenu(View view){
        PopupMenu popupMenu=new PopupMenu(this,view);
        popupMenu.getMenuInflater().inflate(R.menu.menu_each_book_click,popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
                Toast.makeText(getApplicationContext(), "关闭popupMenu", Toast.LENGTH_SHORT).show();
            }
        });
        popupMenu.show();
    }
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/id_item_book_delete"
        android:title="删除" />

    <item
        android:id="@+id/id_item_book_change_name"
        android:title="更改名称" />

    <item
        android:id="@+id/id_item_book_change_img"
        android:title="更改封面" />
</menu>

在按钮中调用showPopupMenu即可

在Fragment中使用toolbar跟activity中不同,直接以控件的形式来使用 https://blog.csdn.net/LABLENET/article/details/54286634

 mainBinding.toolbar.setTitle(getString(R.string.main_index_toolbar_title));
        mainBinding.toolbar.inflateMenu(R.menu.menu_main_mian); //加载菜单
        mainBinding.toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
            //点击事件
                switch (item.getItemId()){
                    case R.id.action_search:

                        break;
                    case R.id.action_chip:

                        break;
                }
                return false;
            }
        });