如何使用V7包中ActionBar(Eclipse版)



原文:http://blog.csdn.net/appte/article/details/11712591


以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目。今年的Google/IO大会之后,Gooogle官方在android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,从此以后曾经最火的Android开源项目ActionBarSherlock可以退出历史舞台了。

要是用V7包中ActionBar也很简单,但有一个需要主要的地方。有些人可能该开始仅仅是把android-support-v7-appcompat.jar导入项目中

具体使用 步骤 (针对于Eclipse):

Create a  library project  based on the support library code:

  1. Make sure you have downloaded the  Android Support Library  using the SDK Manager .
  2. Create a library project and ensure the required JAR files are included in the project's build path:
    1. Select  File > Import .
    2. Select  Existing Android Code Into Workspace  and click  Next .
    3. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompat  project, browse to  <sdk>/extras/android/support/v7/appcompat/ .
    4. Click  Finish  to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat .
    5. In the new library project, expand the  libs/  folder, right-click each .jar  file and select  Build Path > Add to Build Path . For example, when creating the the v7 appcompat project, add both the android-support-v4.jar  and android-support-v7-appcompat.jar  files to the build path.
    6. Right-click the project and select  Build Path > Configure Build Path .
    7. In the  Order and Export  tab, check the  .jar  files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat  project requires you to export both the android-support-v4.jar  and  android-support-v7-appcompat.jar  files.
    8. Uncheck  Android Dependencies .
    9. Click  OK  to complete the changes.

You now have a library project for your selected Support Library that you can use with one or more application projects.

Add the library to your application project:

  1. In the Project Explorer, right-click your project and select  Properties .
  2. In the Library pane, click  Add .
  3. Select the library project and click  OK . For example, the appcompat  project should be listed as  android-support-v7-appcompat .
  4. In the properties window, click  OK .

Once your project is set up with the support library, here's how to add the action bar:

  1. Create your activity by extending  ActionBarActivity .
  2. Use (or extend) one of the  Theme.AppCompat  themes for your activity. For example:
    <activity android:theme="@style/Theme.AppCompat.Light" ... >

Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.

On API level 11 or higher

The action bar is included in all activities that use the  Theme.Holo  theme (or one of its descendants), which is the default theme when either the  targetSdkVersion  or  minSdkVersion  attribute is set to  "11"  or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar .

以上摘自Android官网。

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.folyd.actionbartest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@style/Theme.Base.AppCompat.Light"
            android:name="com.folyd.actionbartest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


package com.folyd.actionbartest;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {
  private ActionBar actionBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
  }

}


效果截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值