ActionBar菜单的特殊用法
ActionProvider:
可以为Action bar的菜单按钮添加子菜单。
新建了一个MyActionProvider继承自ActionProvider,重写hasSubMenu()方法并返回true,然后在onPrepareSubMenu通过调用SubMenu的add()方法添加子菜单。
public class MyActionProvider extends ActionProvider { public MyActionProvider(Context context) { super(context); } @Override public View onCreateActionView() { return null; } @Override public void onPrepareSubMenu(SubMenu subMenu) { subMenu.clear(); subMenu.add("sub item 1").setIcon(R.drawable.ic_launcher) .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return true; } }); subMenu.add("sub item 2").setIcon(R.drawable.ic_launcher) .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return false; } }); } @Override public boolean hasSubMenu() { return true; } }
通过设置android:actionProviderClass将MyActionProvider加入菜单。为了兼容android 2.x,添加xmlns:yourappname="http://schemas.android.com/apk/res-auto",将android:改成yourapp:。
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:helloworld="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/MyActionProvider" android:icon="@drawable/MyActionProvider" android:orderInCategory="1" android:title="MyActionProvider" helloworld:actionProviderClass="com.example.helloworld.MyActionProvider" helloworld:showAsAction="ifRoom"/> </menu>
添加子菜单的另一种方法:在菜单资源文件<item></item>中插入<menu></menu>。
ActionView:
在ActionBar直接显示一个具体的视图。
在menu资源中通过actionViewClass属性来指定一个控件,通常使用SearchView,也可以是自定义控件。为兼容android 2.x,使用SearchView时导入android.support.v7.widget.SearchView,而不是android.widget.SearchView。
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:helloworld="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/CustomView" android:icon="@drawable/CustomView" android:orderInCategory="1" android:title="CustomView" helloworld:actionViewClass="com.example.helloworld.CustomView" helloworld:showAsAction="ifRoom|collapseActionView"/> </menu>
在Activity的onCreateOptionsMenu(Menu menu)函数获取ActionView。
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); MenuItem menuItem = menu.findItem(R.id.CustomView); CustomView customView = (CustomView) menuItem.getActionView(); //设置CustomView ...... return true; }
相关推荐
ActionBar溢出菜单颜色修改 可以参考blog http://blog.csdn.net/luohaowang320/article/details/38236929
ActionBar中的下拉菜单操作
Android 弹无虚发之第二弹:Android ActionBar 的其它用法(搜索、分享、隐藏复杂布局,模仿Google Play,微信)
ActionBar实现分页菜单(ActivityGroup+一些TextView布局.(在这里我们自定实现动态滚动效果))
本文实例讲述了Android ActionBar搜索功能用法。分享给大家供大家参考,具体如下: 使用ActionBar SearchView时的注意点: 首先要吐槽一下Android的官方Guide文档 ,关于用法讲得不明确,可能是一直没更新的原因吧。...
囊括了ActionBar大多的用法,包括搜索按钮常用配置,下拉菜单等。 从配置文件和代码两个方面来说明ActionBar的用法。
ActionBar的各种设置和添加菜单项
actionbar中增加setting菜单,用PopupWindow实现的,代码简单
actionBar 高级用法,基本用到的ActionBar的常规操作,源于大家共勉
Android自定义ActionBar示例。 用法参考: http://blog.csdn.net/zircon_1973/article/details/7680404
在ActionBar的右侧,可以是在选项菜单(options menu)中任意一个菜单项。ActionBar提供若干有用的特性,其中包括了: ① 作为“action选项”,在ActionBar上直接显示“选项菜单”(OptionsMenu)—— 为用户进行...
android仿微信6.0底部滑动菜单栏+导航栏,ViewPager+RadioGroup+ActionBar. 有博客描述
包括2个代码例子,一个是自定义 actionbar 适合用于3.0以下版本也需要使用actionbar。一个例子是 修改actionbar style样式,如果替换背景等。
这是我查看官方帮助文档完成的一个关于actionBar的例子,包括自定义菜单,和用actionBar实现菜单导航功能。
actionbar+fragment实现顶部导航菜单的功能
popwindows实现actionbar上的小菜单的源码,具体用法详见博客。
侧拉菜单时actionbar图标做动画
android-simple-actionbar-app 简单的Android ActionBar / Toolbar示例用法
Dropdown菜单也常被用在ActionBar中,下面的代码介绍了用法.