您好,登錄后才能下訂單哦!
如何在Android項目中動態修改ToolBar中的Menu菜單欄?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
Android動態修改ToolBar的Menu菜單
實現很簡單,就是一個具有3個Action的Menu,在我們滑動到不同狀態的時候,把對應的Action隱藏了。
開始上貨
Menu
Menu下添加3個Item
<menu 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" tools:context="com.kongqw.myapplication.MainActivity"> <item android:id="@+id/search_button" android:icon="@mipmap/navigation_icon_search" android:orderInCategory="100" android:title="搜索" app:showAsAction="ifRoom" /> <item android:id="@+id/scan_button" android:icon="@mipmap/navigation_icon_sweep" android:orderInCategory="100" android:title="掃描" app:showAsAction="ifRoom" /> <item android:id="@+id/setting_button" android:icon="@mipmap/titlebar_icon_settings_normal" android:orderInCategory="100" android:title="設置" app:showAsAction="ifRoom" /> </menu>
重寫onPrepareOptionsMenu方法
用來根據ViewPager顯示頁面控制Menu顯示的Item
Demo里我用的是ViewPager滑動來控制,可以根據自己需求,不過大多數都是判斷ViewPager吧。
@Override public boolean onPrepareOptionsMenu(Menu menu) { // 動態設置ToolBar狀態 switch (mViewPager.getCurrentItem()) { case 0: menu.findItem(R.id.search_button).setVisible(true); menu.findItem(R.id.scan_button).setVisible(true); menu.findItem(R.id.setting_button).setVisible(false); break; case 1: menu.findItem(R.id.search_button).setVisible(false); menu.findItem(R.id.scan_button).setVisible(false); menu.findItem(R.id.setting_button).setVisible(false); break; case 2: menu.findItem(R.id.search_button).setVisible(false); menu.findItem(R.id.scan_button).setVisible(false); menu.findItem(R.id.setting_button).setVisible(true); break; } return super.onPrepareOptionsMenu(menu); }
Item點擊
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.search_button) { Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show(); return true; } else if (id == R.id.scan_button) { Toast.makeText(getApplicationContext(), "掃描", Toast.LENGTH_SHORT).show(); return true; } else if (id == R.id.setting_button) { Toast.makeText(getApplicationContext(), "設置", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); }
監聽ViewPager滑動,改變Menu狀態(重點)
調用invalidateOptionsMenu();方法從新加載Menu,即回調onPrepareOptionsMenu方法
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); invalidateOptionsMenu(); } @Override public void onPageScrollStateChanged(int state) { } });
Code(參考)
就是Android Studio默認創建的帶有ViewPager的Demo,
package com.kongqw.myapplication; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); invalidateOptionsMenu(); } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { // 動態設置ToolBar狀態 switch (mViewPager.getCurrentItem()) { case 0: menu.findItem(R.id.search_button).setVisible(true); menu.findItem(R.id.scan_button).setVisible(true); menu.findItem(R.id.setting_button).setVisible(false); break; case 1: menu.findItem(R.id.search_button).setVisible(false); menu.findItem(R.id.scan_button).setVisible(false); menu.findItem(R.id.setting_button).setVisible(false); break; case 2: menu.findItem(R.id.search_button).setVisible(false); menu.findItem(R.id.scan_button).setVisible(false); menu.findItem(R.id.setting_button).setVisible(true); break; } return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.search_button) { Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show(); return true; } else if (id == R.id.scan_button) { Toast.makeText(getApplicationContext(), "掃描", Toast.LENGTH_SHORT).show(); return true; } else if (id == R.id.setting_button) { Toast.makeText(getApplicationContext(), "設置", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); } public static class PlaceholderFragment extends Fragment { private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); TextView textView = (TextView) rootView.findViewById(R.id.section_label); textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; } } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "SECTION 1"; case 1: return "SECTION 2"; case 2: return "SECTION 3"; } return null; } } }
關于如何在Android項目中動態修改ToolBar中的Menu菜單欄問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。