目前不少APP的頭部導航都是非導航類似的,爲了簡化開發,下降耦合,因此一直在使用封裝以後的NavigationBar。java
github地址git
1,從左側添加menu,支持多個。(menu能夠是string,drawable,layout) 2,從右側添加menu,支持多個。(menu能夠是string,drawable,layout) 3,設置中間的文字title。 4,給title設置 drewable left,top,right,bottom。 5,用自定義View替換title view。 6,添加/移除 top layer view。 7,統一堅挺menu點擊事件。github
NavigationBar navBar1 = (NavigationBar) findViewById(R.id.nav_bar_1); navBar1.setTitle("Nav1");//中間標題 navBar1.addFromLeft(NavItems.search_img);//左側添加可多個 navBar1.addFromRight(NavItems.search_txt);//右側添加可多個 navBar1.setNavgationListener(navgationListener); // NavigationBar navBar2 = (NavigationBar) findViewById(R.id.nav_bar_2); navBar2.setTitle("Nav2"); navBar2.addFromLeft(NavItems.search_img);//左側1 navBar2.addFromLeft(NavItems.save);//左側2 navBar2.addFromRight(NavItems.messgae_count_view);//右側自定義View navBar2.setNavgationListener(navgationListener); // NavigationBar navBar3 = (NavigationBar) findViewById(R.id.nav_bar_3); navBar3.setTitle("View Title"); SearchBar mSearchBar = createSearchBar();//自定義View navBar3.replaceTitleView(mSearchBar);//替換到標題位置 //navBar3.addFromLeft(NavItems.save);//同時支持左右添加 navBar3.setNavgationListener(navgationListener); //-----其餘經常使用函數----- //添加整個導航層View //navBar3.addTopLayer(mSearchBar); //設置title的drawable //navBar3.setTitleTextViewDrawable(left,top,right,bottom); //使用Layout Id直接替換title //navBar3.replaceTitleView(R.layout.xxx); //經過res設置title //navBar3.setTitle(R.string.app_name); //設置item是否可見 //navBar3.changeNavChildVisiable(itemId,visiable);
menu item監聽app
private NavgationListener navgationListener = new NavgationListener() { @Override public void onNavigationClick(View v, NavItem item, NavigationBar nav) { if (item.getId() == NavItems.search_img.getId()) { toast("img(Search)"); } else if (item.getId() == NavItems.search_txt.getId()) { toast("txt(Search)"); } else if (item.getId() == NavItems.messgae_count_view.getId()) { toast("view(MsgCount)"); } else if (item.getId() == NavItems.save.getId()) { toast("txt(Save)"); } } };
menu item定義ide
public class NavItems { public static final NavItem search_img = BaseNavItem.img(R.drawable.nav_icon_search); public static final NavItem search_txt = BaseNavItem.text("Search"); public static final NavItem save = BaseNavItem.text("Save"); public static final NavItem messgae_count_view = BaseNavItem.view(R.layout.nav_item_msg_count); }
一般引入NavigationBar以後只須要定義項目須要用到的Item便可。函數