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);
}
}

最新文章

  1. csv表格处理(上)-- JS 与 PHP 协作导入导出
  2. php常用方法总结
  3. 3.Factory Method 工厂方法模式(创建型模式)
  4. uTenux&mdash;&mdash;重新整理底层驱动库
  5. 9. shell环境
  6. 关于Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (2)的问题
  7. 2013ACM/ICPC亚洲区南京站现场赛——题目重现
  8. Java之--Java基础知识
  9. zoom与transform:scale的区别
  10. 【转】WF4.0 (基础篇)
  11. POJ2728 最小比率生成树/0-1分数规划/二分/迭代(迭代不会)
  12. Windows phone 8 学习笔记(8) 定位地图导航
  13. 集束搜索beam search和贪心搜索greedy search
  14. sqlparameters
  15. python之format函数
  16. Ext数字格式化
  17. 【读书笔记】iOS-库
  18. C#定时备份正在播放的幻灯片、word文档、excel电子表格,mht格式文档
  19. Linux IO实时监控iostat命令
  20. C++进阶--命名空间和关键字using

热门文章

  1. jq简单选项卡
  2. Unity3D之Mecanim动画系统学习笔记(五):Animator Controller
  3. C#中的强制类型转换与as转换的区别
  4. python中List操作
  5. 【转】WPF颜色相关操作
  6. iOS 使用compare 进行对比
  7. VHD_Update_mount-vhd
  8. 【转】makefile语法规则
  9. QT5 串口收发实例代码
  10. iOS开发——Metal教程