`
qilixiang012
  • 浏览: 203084 次
文章分类
社区版块
存档分类
最新评论

android 实战 AppCompat实现Action Bar

 
阅读更多

每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计。如果对ActionBar不太熟悉的可以参考《

Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得Android 2.1以上的Android应用使用actionbarsherlock定义的Action Bar。这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat使得Android2.1以上的版本可以实现Action Bar。


Google I/O 2013中AppCompat实现的Action Bar效果


AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以库的形式引入到应用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/的位置,需要自行下载,或者升级SDK。

如果应用是使用actionbarsherlock实现的Action Bar,也不必刻意的改成AppCompat。因为actionbarsherlock是一个很稳定的经过很多开发者验证的开发库。

ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.   
If you are already using it and do not currently require any of the above then there is no need to migrate.


1)导入AppCompat库

使用AppCompat第一步需要导入AppCompat库,这一步就不做详细介绍了。


2)修改android:theme

每个使用Action Bar的Activity都应该添加Android:theme

<activity  
        ...  
        android:theme="@style/Theme.AppCompat" /> 

或者修改application

<application  
       android:label="@string/app_name"  
       android:icon="@drawable/ic_launcher"  
       android:theme="@style/Theme.AppCompat"   
       android:allowBackup="true">  

3)Activity要继承自ActionBarActivity

实现Action Bar的视图需要继承ActionBarActivity。


4)修改menu的命名空间


<menu xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:holo="http://schemas.android.com/apk/res-auto" >  
  
    <item  
        android:id="@+id/action_websearch"  
        android:icon="@drawable/action_search"  
        android:title="@string/action_websearch"  
        holo:showAsAction="never"/>  
</menu>  

要特别注意的是,通过XML文件来实现Action Item,一定要自定义命名空间,而且该命名空间的后缀一定要和item中showAsAction的前缀一致,本例中为“holo”

显示Menu需要重写onCreateOptionsMenu方法:

@Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
       MenuInflater inflater = getMenuInflater();  
       inflater.inflate(R.menu.main, menu);  
       return super.onCreateOptionsMenu(menu);  
   }  

对Menu的item事件处理需要重写onOptionsItemSelected方法。

@Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
         // The action bar home/up action should open or close the drawer.  
         // ActionBarDrawerToggle will take care of this.  
        if (mDrawerToggle.onOptionsItemSelected(item)) {  
            return true;  
        }  
        // Handle action buttons  
        switch(item.getItemId()) {  
        case R.id.action_websearch:  
            // create intent to perform web search for this planet  
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);  
            intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());  
            // catch event that there's no activity to handle intent  
            if (intent.resolveActivity(getPackageManager()) != null) {  
                startActivity(intent);  
            } else {  
                Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();  
            }  
            return true;  
        default:  
            return super.onOptionsItemSelected(item);  
        }  
    }  

上面就是简单的通过Appcompat实现Action Bar,想自定义各种属性请参考官方文档。


demo下载:demo

/**
* @author 张兴业
* iOS入门群:83702688
* android开发进阶群:241395671
* 我的新浪微博:@张兴业TBOW
*/

参考:

http://antonioleiva.com/actionbarcompat-how-to-use/

http://antonioleiva.com/actionbarcompat-action-views/

http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html



分享到:
评论

相关推荐

    android-support-v7-appcompat.zip

    给需要的人。看了别人的动不动都要好多分。无语。因为没分下东西,就设置1分

    appcompatdemo

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar...这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat 实现Action Bar。

    Android中Action Bar的使用

    Android中Action Bar的使用

    android action bar例子

    android action bar例子

    android_action-bar

    android add action bar by mutiple

    Action Bar Icon Pack

    This is the Action Bar Icon Pack that is provided as part of Android Design Guidelines.

    自定义Android Action Bar的布局

    一个简单的列子展示了如何自定义ActionBar的布局,详细的介绍可以查看我的Blog

    Android设置theme中可能遇到的坑

    This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR 原因一 错误写法: &lt;style name=AppTheme.NoActionBar&gt; &lt;item name=android:...

    Action bar 操作栏

    操作栏是一个窗口特性,确定用户的位置,和 提供用户操作和导航模式。 使用操作栏为用户提供了一个 跨应用程序系统优雅地适应熟悉的界面 对于不同的屏幕配置。

    Android代码-一种简便、可变Action的实现方案

    Android Dynamic Action,简称DA,是一种简便、可变Action的实现方案。DA框架的初衷是为了取代Context.startActivity的调用方式,使用建造者模式(Builder Pattern)构建交互参数,使程序更优美。DA框架能够对任何一个...

    Android实战(第三版)英文电子版

    Android in Action, Third Edition takes you far beyond "Hello Android." You'... This book also introduces important tablet concepts like drag-and-drop, fragments, and the Action Bar, all new in Android 3

    Android Action Bar 详解篇(推荐)

    它可以作为活动的标题,突出活动的一些关键操作(如“搜索”、“创建”、“共享”等)、作为菜单的灵活使用,还可以实现类似TabWidget的标签功能以及下拉导航的功能,系统能够很好根据不同的屏幕配置来适应ActionBar...

    Android action bar

    Android action bar 用法,以及如何使用,是网上流行的代码,不是原著。

    Andriod接收广播的类

    恢复已经停止的更新下载。 'android.server.checkin.FOTA_RESTART' 通过 OTA 下载并安装操作系统更新。 'android.server.checkin.FOTA_UPDATE' 用户按下了'Media Button'。...'android.intent.action.PACKAGE_ADDED'

    android 常用的intent action整理

    Android Itent Action 小结

    Android 广播大全 Intent Action 事件.

    Android 广播大全 Intent Action 事件.

    Android UI Action Bar之ActionBarSherlock

    NULL 博文链接:https://rensanning.iteye.com/blog/2005174

    Android应用开发中Action bar编写的入门教程

    从Android 3.0开始除了我们重点讲解的Fragment外,Action Bar也是一个重要的内容,Action Bar主要是用于代替传统的标题栏,对于Android平板设备来说屏幕更大它的标题使用Action Bar来设计可以展示更多丰富的内容,...

    模拟Action Bar.zip

    该代码简洁明了,能够实现模拟Action Bar的功能,且有相应的注释,易于读者理解。

Global site tag (gtag.js) - Google Analytics