使用命令行创建的activuty默认是继承自Activity,然而在学习google traning中

http://developer.android.com/training/basics/firstapp/starting-activity.html

一节的时候发现示例继承的是ActionBarActivity。这是一个来自与Android Support Library的类,主要是为了兼容Android 3.0以下的版本。

关于action bar的内容traning中后面一节有更多介绍

http://developer.android.com/training/basics/actionbar/setting-up.html


为了使用Android Support Library,官网上提供了IDE的配置方法

http://developer.android.com/tools/support-library/setup.html


我用的是maven命令行的方式,ActionBarActivity来自support-v7-appcompat,但没有现成的maven dependency可用,所以得自己安装到本地仓库。参考了下面几个网页,

http://stackoverflow.com/questions/18419806/generate-apklib-of-compatibility-v7-appcompat/18796764#18796764

http://stackoverflow.com/questions/18025942/how-do-i-add-a-library-android-support-v7-appcompat-in-intellij-idea

https://code.google.com/p/maven-android-plugin/wiki/ApkLib


具体配置方法如下:


首先得在Android SDK Manager的extras中安装Android Support Library

安装好后可以在/extras/android/support/v7中看到appcompat、gridlayout、mediarouter三个文件夹,我要用的是appcompat这个库。


进入appcompat这个文件夹,将里面的内容打包成zip文件,再重命名为appcompat.apklib。运行

mvn install:install-file -Dfile=appcompat.apklib -DgroupId=com.google.android -DartifactId=support-v7-appcompat -Dversion=r7 -Dpackaging=apklib


因为这个库还依赖了其他两个jar包,/extras/android/support/v7/appcompat/libs中的android-support-v4.jar和android-support-v7-appcompat.jar,所以还得将它们一起安装到本地仓库。进入libs文件夹,运行

mvn install:install-file -Dfile=android-support-v7-appcompat.jar -DgroupId=com.google.android -DartifactId=support-v7-appcompat -Dversion=r7 -Dpackaging=jarmvn install:install-file -Dfile=android-support-v4.jar -DgroupId=com.google.android -DartifactId=support-v4 -Dversion=r7 -Dpackaging=jar


然后就可以在自己项目的pom文件里添加依赖

            com.google.android        support-v7-appcompat        r7        apklib                com.google.android        support-v4        r7        jar                com.google.android        support-v7-appcompat        r7        jar    

除了配置依赖,还得修改自己的项目,需要在AndroidManifest文件中,为使用的ActionBarActitity添加theme属性,这个theme必须是 @style/Theme.AppCompat 或它的子类

              


最后还得在AndroidManifest中设置你需要的api level,为了能运行在Android 2.1以上,所以设置了minSdkVersion为7

 


最后贴上完整的DisplayMessageActivity.java

package com.example.myfirstapp;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.widget.TextView;import android.content.Intent;public class DisplayMessageActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);    // Get the message from the intent    Intent intent = getIntent();    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);    // Create the text view    TextView textView = new TextView(this);    textView.setTextSize(40);    textView.setText(message);    // Set the text view as the activity layout    setContentView(textView);    }}

编译、安装、部署,就可以看到traning中的效果了,我用的是真机测试,就不截图了。


更多相关文章

  1. Android(安卓)Studio3.0之后gradle.build的修改(implementation,
  2. Android(安卓)调用已安装市场,去应用市场评分
  3. Android(安卓)Studio 安装完成不能打开的方法
  4. MAC 安装Eclipse: Failed to get the required ADT version numb
  5. cubieboard刷机
  6. [React-Native]环境安装问题
  7. 一、开发环境搭建及配置
  8. 【Android】LitePal安装和使用
  9. 初学Android(一)

随机推荐

  1. 记录一次九月份腾讯 Android 面试笔试总
  2. ANDROID抽屉式左右侧滑菜单DEMO源代码下
  3. Android开发实践:基于命令模式的异步任务
  4. 浅析Context及可能带来的内存泄漏问题
  5. Android 事件处理(—)(附源码)
  6. iPhone和Android智能手机浏览器判断分辨
  7. android 开关按钮
  8. 基于移动平台的多媒体框架——移植ffmpeg
  9. Android中抓取手机视频流数据。
  10. android的Activity窗口模式