Android菜鸟的成长笔记(28)——Google官方对Andoird 2.x提供的ActionBar支持

来源:互联网 发布:mysql 存储过程 函数 编辑:程序博客网 时间:2024/04/30 13:44

在Google官方Android设计指南中(链接:http://www.apkbus.com/design/get-started/ui-overview.html)有一个新特性就是自我标识,也就是宣传自己,所以很多应用现在也自然的使用ActionBar并提供自己的logo.

微信的应用:


Google的Android设计指南中是这样说的:应用的 启动图标 作为启动应用的入口是展示 logo 的最佳场所。你也可以将启动图标放置在 操作栏 上,从而保证在应用内的所有页面上都能看到它。

在使用ActionBar的时候,会发现一个问题。在3.0以前SDK中是不支持ActionBar的,所以如果手机apk要兼容2.2或2.3的手机就需要用一个开源的项目ActionBarSherlock,具体使用方法如下:

1、下载开源包:http://actionbarsherlock.com/usage.html

2、导入到Eclipse中(和导入项目步骤相同,记得勾选Is Library)


3、在项目中引用(properties->android->add  加进去


4、修改主题为@Style/Theme.Sherlock.Light(或其子类)

5、继承SherlockActivity。

6、使用getSupportActionBar()获取ActionBar对象。

上面方法就可以实现低版本使用ActionBar的问题,但是Goole去年推出了自己的兼容包,使用起来更加方便。下面我们就来看看如何使用support_v7。

1、和上面一样下载和导入appcompat_7.x兼容包(如果是官方最新的sdk开发工具则提供)

2、在项目中引用:


3、修改主题为@style/Theme.AppCompat(或其子类)

4、修改menu/目录下对应的xml文件

<?xml version="1.0" encoding="utf-8"?>  <menu xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:alpha="http://schemas.android.com/apk/res-auto">       <!-- Search, should appear as action button -->        <item android:id="@+id/action_search"              android:icon="@drawable/ic_action_refresh"              android:title="刷新"             alpha:showAsAction="always"/>        <!-- Settings, should always be in the overflow -->      <item android:id="@+id/action_add"              android:title="分享"              android:icon="@drawable/ic_action_share"            alpha:showAsAction="always" />             <item android:id="@+id/action_settings"              android:title="更多"              android:icon="@drawable/ic_action_overflow"            alpha:showAsAction="always">             <menu >                <group >                    <item                         android:id="@+id/item1"                        android:title="个人中心"                        android:icon="@drawable/ic_action_share"/>                    <item                         android:id="@+id/item2"                        android:title="设置"                        android:icon="@drawable/ic_action_share"/>                    <item                         android:id="@+id/exit_system"                        android:title="退出"                        android:icon="@drawable/ic_action_share"/>                </group>            </menu>      </item>   </menu>  
5、继承自ActionBarActivity

6、使用getSupportActionBar获取ActionBar对象。

ActionBar actionBar = getSupportActionBar();actionBar.setDisplayShowHomeEnabled(true);actionBar.setIcon(R.drawable.actionbar_icon);

在Android 2.2和2.3手机上完美运行...


13 0