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

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</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

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

ActionBar的搜索功能

在Activity中,增加以下代码:

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

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. Thinkphp框架 -- ajax无刷新上传图片

    用Thinkphp框架做无刷新上传图片 视图层 View <!doctype html> <html lang="en"> <head> < ...

  2. 使用Unity制作游戏关卡的教程(二)

    转自:http://gamerboom.com/archives/75554 作者:by Matthias Zarzecki 本文是“使用Unity制作<The Fork Of Truth> ...

  3. delphi 取得汉字的第一个字母

    功能说明://取得汉字的第一个字母 function GetPYIndexChar( hzchar:string):char;begin  caseWORD(hzchar[1])shl8+WORD(h ...

  4. Nginx 1.4.7图片缓存服务器

    软件包版本: Nginx 1.4.7 Ngx_cache_purge-2.0 Openssl-1.0.1 Pcre-8.32 二.安装编译: a)         下载pcre-8.32.tar.gz ...

  5. 个人对maven pom.xml文件的理解

    如:一个项目可能需要引用另外两个项目的类.. 如 项目cswebbefore  需要引用cswebservice 和reports 这三个项目都有各自的pom.xml文件 cswebservice 项 ...

  6. Java 计算两个日期相差月数

    package com.myjava; import java.text.ParseException;import java.text.SimpleDateFormat;import java.ut ...

  7. 关于AutoComplete整合

    AutoComplete应该不是很陌生了,网上也有好多开源的js.今天主要的不是研究Autocomplete这个js的实现.今天主要讲的是将这个js做成一插件.那么今天主要用到的 js插件jquery ...

  8. c#线程问题(2)

    表示要在新上下文中调用的方法.public delegate void ContextCallback(Object state) 参数 state 类型: System. Object一个对象,包含 ...

  9. 步进循环语句for

    一.for语句的基本格式 for 变量 in 列表 do 语句块 done 二.使用for语句处理列表(数组) [root@localhost shell]# cat use_for_deal_wit ...

  10. Qt之遍历文件夹

    关于Qt操作文件夹.文件的知识用途较多,比如遍历下一层乃至所有子孙文件.文件夹,获取它们的一些信息(大小.类型.最后更改时间等).当然,也可以进行级联删除.     首先看简单的:   一.Qt遍历文 ...