About ActionBar

The action bar is one of the most important design elements you can implement for your app's activities. It provides several user interface features that make your app immediately familiar to users by offering consistency between other Android apps. Key functions include:

A dedicated space for giving your app an identity and indicating the user's location in the app.

Access to important actions in a predictable way (such as Search).

Support for navigation and view switching (with tabs or drop-down lists).

ActionBar 的创建

如果最低兼容版本小于3.0 --> Support Android 2.1 and Above

Setting Up the Action Bar

1.引用V7-appcompat
>   To get started, read the Support Library Setup document and set up the v7 appcompat library
2.Activity继承ActionBarActivity 
> Update your activity so that it extends ActionBarActivity
3.在配置清单文件中,android:theme="@style/Theme.AppCompat.Light"
> In your manifest file, update either the <application> element or individual <activity> elements to use one of the Theme.AppCompat themes.

Adding Action Buttons

Add an <item> element for each item you want to include in the action bar. For example:

res/menu/main_activity_actions.xml

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
  3. <!-- Search, should appear as action button -->
  4. <item android:id="@+id/action_search"
  5. android:icon="@drawable/ic_action_search"
  6. android:title="@string/action_search"
  7. yourapp:showAsAction="ifRoom" />
  8. ...
  9. </menu>

如果最低兼容版本大于3.0 --> Support Android 3.0 and Above Only

Setting Up the Action Bar

在配置清单文件中,android:theme="@android:style/Theme.Holo..."

Adding Action Buttons

Add an <item> element for each item you want to include in the action bar. For example:

res/menu/main_activity_actions.xml

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  2. <!-- Search, should appear as action button -->
  3. <item android:id="@+id/action_search"
  4. android:icon="@drawable/ic_action_search"
  5. android:title="@string/action_search"
  6. android:showAsAction="ifRoom" />
  7. <!-- Settings, should always be in the overflow -->
  8. <item android:id="@+id/action_settings"
  9. android:title="@string/action_settings"
  10. android:showAsAction="never" />
  11. </menu>

ActionBar的搜索功能

在Activity中,增加以下代码:

  1. public boolean onCreateOptionsMenu(Menu menu) {
  2. MenuInflater inflater = getMenuInflater();
  3. inflater.inflate(R.menu.activity_main, menu);
  4.  
  5. // 不兼容低版本
  6. SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
  7. searchView.setOnQueryTextListener(this); // 搜索的监听
  8.  
  9. return super.onCreateOptionsMenu(menu);
  10. }
  1. public boolean onOptionsItemSelected(MenuItem item) {
  2. // Handle presses on the action bar items
  3. switch (item.getItemId()) {
  4. case R.id.action_search:
  5. openSearch();
  6. return true;
  7. default:
  8. return super.onOptionsItemSelected(item);
  9. }
  10. }

ActionBar 的简单使用的更多相关文章

  1. ActionBar的简单使用

    只简单实现了一下ActionBar的使用,在右上角添加两个ActionBar,在左上角实现默认的返回箭头,类似于微信朋友圈的 这是MainActivity的代码: public class MainA ...

  2. 安卓---achartengine图表----简单调用----使用view显示在自己的布局文件中----actionBar的简单设置

    AChartEngine 是一个安卓系统上制作图表的框架,关于它的介绍大家可以百度,也可以参考下面这篇博客http://blog.csdn.net/lk_blog/article/details/76 ...

  3. [Android]AndroidDesign中ActionBar探究2 嵌入Fragment

    上一节我们只是简单了介绍了Android Design风格中的ActionBar的简单实用,如添加MenuItem,这节我们会进一步了解ActionBar的其他功能. 在Android Develop ...

  4. Android M 控件:Snackbar、Toolbar、TabLayout、NavigationView

    Snackbar Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈.Snackbar的使用与Toast的使用基本相同: Snack ...

  5. 001.android初级篇之ToolBar

    官方的最新support library v7中提供了新的组件ToolBar,用来替代之前的ActionBar,实现更为弹性的设计在 material design 也对之做了名称的定义:App ba ...

  6. 美好头标ToolBar

    ActionBar我相信是每一位合格的程序员都用过的组件,也是每一个程序员都会抱怨的组件,因为他不能实现复杂的自定义.为此Google推出了比ActionBar更为美好的组件ToolBar. 本文重点 ...

  7. Android Material Design之Toolbar与Palette

    转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...

  8. android Material Design详解

    原文地址:http://blog.csdn.net/jdsjlzx/article/details/41441083/ 前言 我们都知道Marterial Design是Google推出的全新UI设计 ...

  9. [安卓] 18、一个简单的例子做自定义动画按钮和自定义Actionbar

    在做安卓UI的时候有时候需自定义具有动画效果的按钮或需要自定义一下actionbar~ 本节用一个简单的demo讲如何自定义具有动画效果的按钮,以及个性化的actionbar 下面是效果: 其中: △ ...

随机推荐

  1. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  2. 使用Filter防止浏览器缓存页面或请求结果

    仅仅须要两步: 1.定义一个Filter: /** * 防止浏览器缓存页面或请求结果 * @author XuJijun * */ public class NoCacheFilter impleme ...

  3. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  4. HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和

    To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  5. delphi 11 编辑模式 浏览模式

    编辑模式 浏览模式 设置焦点 //在使用前需要Webbrowser已经浏览过一个网页 否则错误 uses MSHTML; ///获取Webbrowser编辑模式里面的内容procedure EditM ...

  6. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  7. SVN Cleanup failed的解决办法

    一开始没有更新执行了提交操作,提示有冲突 再执行更新操作的时候出现了“之前操作未完成,如果该操作被中断了执行cleanup命令”的提示

  8. python 学习(一)

    python的基础看完了之后,有点像简化并提供了一定优化后的java基础,看java多了的人看python还是比较别扭的.看完别人对于java和python的对比,我只能感慨一句,还有什么是java办 ...

  9. UNIX基础知识之信号

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 信号(signal)是通知进程已发生某种情况的 ...

  10. Python_爬虫2

    URLError异常处理 大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本 ...