以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目。今年的Google/IO大会之后,Google官方在Android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,从此以后曾经最火的Android开源项目ActionBarSherlock可以退出历史舞台了。
要是用V7包中ActionBar也很简单,但有一个需要注意的地方。有些人可能刚开始仅仅是把android-support-v7-appcompat.jar导入项目中,但是在设置Activity的theme时会报错,提示找不到"@style/Theme.AppCompat"。这是由于我们要把v7和资源文件一起导入才行。 具体使用步骤(针对于Eclipse): Create a library project based on the support library code:
Make sure you have downloaded the Android Support Library using the SDK Manager. Create a library project and ensure the required JAR files are included in the project's build path: Select File > Import. Select Existing Android Code Into Workspace and click Next. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompat project, browse to /extras/android/support/v7/appcompat/. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat. 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 andandroid-support-v7-appcompat.jar files to the build path. Right-click the project and select Build Path > Configure Build Path. 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. Uncheck Android Dependencies. 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:
In the Project Explorer, right-click your project and select Properties. In the Library pane, click Add. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat. In the properties window, click OK. Once your project is set up with the support library, here's how to add the action bar:
Create your activity by extending ActionBarActivity. Use (or extend) one of the Theme.AppCompat themes for your activity. For example:
要是用V7包中ActionBar也很简单,但有一个需要注意的地方。有些人可能刚开始仅仅是把android-support-v7-appcompat.jar导入项目中,但是在设置Activity的theme时会报错,提示找不到"@style/Theme.AppCompat"。这是由于我们要把v7和资源文件一起导入才行。 具体使用步骤(针对于Eclipse): Create a library project based on the support library code:
Make sure you have downloaded the Android Support Library using the SDK Manager. Create a library project and ensure the required JAR files are included in the project's build path: Select File > Import. Select Existing Android Code Into Workspace and click Next. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompat project, browse to /extras/android/support/v7/appcompat/. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat. 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 andandroid-support-v7-appcompat.jar files to the build path. Right-click the project and select Build Path > Configure Build Path. 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. Uncheck Android Dependencies. 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:
In the Project Explorer, right-click your project and select Properties. In the Library pane, click Add. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat. In the properties window, click OK. Once your project is set up with the support library, here's how to add the action bar:
Create your activity by extending ActionBarActivity. Use (or extend) one of the Theme.AppCompat themes for your activity. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <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官网。 <pre class = "\"brush:cpp;toolbar:false\"" ><!--?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" > </category></action></intent-filter> </activity> </application> </uses-sdk></manifest> <pre class = "\"brush:cpp;toolbar:false\"" >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 ); } } </pre> <p>本文链接:<a href= "https://it72.com/9469.htm" >https://it72.com/9469.htm</a></p> </pre></activity> |
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2300
- 用户1336
- 访客10859999
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员