ActionBar使用要点
2016-01-18 07:57
691 查看
ActionBar是用于实现各种效果及快捷操作的动作栏,通常位于顶部状态栏的下方。
其演示效果如下所示:
![](https://img-blog.csdn.net/20160118080115412)
![](https://img-blog.csdn.net/20160118080140629)
![](https://img-blog.csdn.net/20160118080200058)
可以看出,ActionBar主要包含以下3部分:
Icon和Title
Action Items
Action Overflow
在建立好工程后的res/styles.xml的style标签中的parent属性中设定”android:Theme.Holo”或其子Theme。
在建立好的工程后的res/styles.xml的style标签中的parent属性中设定内容为”android:Theme.Holo.NoActionBar”;
在onCreate方法中添加代码
在AndroidManiFest.xml中的activity标签中设置android:logo属性。
修改Title:
在AndroidManiFest.xml中的activity标签中设置android:label属性。
android:icon——设置Action Items或Action Overflow的图标;
android:title——设置Action Items或Action Overflow的标题;
android:showAsAction
-always——永远显示在Action Items中;
-ifRoom——若Action Bar空间足够,则显示在Action Items上,否则显示在Action Overflow中;
-never——只显示在Action Overflow中。
重写onMenuItemSelected方法,为item项添加点击事件:
其演示效果如下所示:
可以看出,ActionBar主要包含以下3部分:
Icon和Title
Action Items
Action Overflow
设置、添加、移除ActionBar的内容
ActionBar的整体添加:
在建立Android工程时,在Theme中选择”Holo Light with Dark Action Bar”;在建立好工程后的res/styles.xml的style标签中的parent属性中设定”android:Theme.Holo”或其子Theme。
ActionBar的整体移除:
在建立工程时,在Theme中选择”None”;在建立好的工程后的res/styles.xml的style标签中的parent属性中设定内容为”android:Theme.Holo.NoActionBar”;
在onCreate方法中添加代码
ActionBar actionBar = getActionBar(); actionBar.hide();
设置Icon和Title
修改Icon:在AndroidManiFest.xml中的activity标签中设置android:logo属性。
修改Title:
在AndroidManiFest.xml中的activity标签中设置android:label属性。
设置Action Items 和Action Overflow
应把所有的Action Items和Action Overflow设置在一个menu资源文件夹中(新建的xml文件以menu为根元素),每个item标签对应一个Action Items或Action Overflow,item标签有如下常用属性:android:icon——设置Action Items或Action Overflow的图标;
android:title——设置Action Items或Action Overflow的标题;
android:showAsAction
-always——永远显示在Action Items中;
-ifRoom——若Action Bar空间足够,则显示在Action Items上,否则显示在Action Overflow中;
-never——只显示在Action Overflow中。
为Action Items 和Action Overflow添加点击事件
重写onCreateOptionMenu方法,加载menu布局文件:@Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater _inflater = getMenuInflater(); _inflater.inflate(R.menu.menu_items, menu); return super.onCreateOptionsMenu(menu); }
重写onMenuItemSelected方法,为item项添加点击事件:
@Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.ok: Toast.makeText(this, "choose ok", Toast.LENGTH_SHORT).show(); break; case R.id.delete: Toast.makeText(this, "choose delete", Toast.LENGTH_SHORT).show(); break; case R.id.email: Toast.makeText(this, "choose email", Toast.LENGTH_SHORT).show(); default: break; } return super.onMenuItemSelected(featureId, item); }
相关文章推荐
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- 修正IE下使用CSS属性overflow的bug
- 用margin和overflow属性实现div间距的方法
- 灵活使用Android中ActionBar和ViewPager切换页面
- Android中ActionBar以及menu的代码设置样式
- android中开启actionbar的两种方法
- ActionBar的返回按钮问题
- ActionBar+Fragment实现Tab
- 积累总结:overflow的CSS属性
- 点击切换样式表实现内容折叠与展开(overflow)
- Android - Toolbar 神秘的左margin
- 如何让有物理键的手机在ActionBar始终显示更多菜单menu键
- ActionBar 值 addTab 的小提示
- ActionBar之style出现Cannot resolve symbol 'Theme' 错误
- bulk传输bushound显示buffer overrun
- 自定义ActionBar或Toolbar
- android 里的actionbar最右边的overflow功能的实现
- parse in programming
- 滚动条
- ActionBar(菜单活动栏)一些常用属性基本使用