使用命令行创建的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
安装好后可以在<sdk>/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包,<sdk>/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=jar
mvn install:install-file -Dfile=android-support-v4.jar -DgroupId=com.google.android -DartifactId=support-v4 -Dversion=r7 -Dpackaging=jar
然后就可以在自己项目的pom文件里添加依赖
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v7-appcompat</artifactId>
<version>r7</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r7</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v7-appcompat</artifactId>
<version>r7</version>
<type>jar</type>
</dependency>
除了配置依赖,还得修改自己的项目,需要在AndroidManifest文件中,为使用的ActionBarActitity添加theme属性,这个theme必须是 @style/Theme.AppCompat 或它的子类
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity"
android:theme="@style/Theme.AppCompat.Light">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
最后还得在AndroidManifest中设置你需要的api level,为了能运行在Android 2.1以上,所以设置了minSdkVersion为7
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" />
最后贴上完整的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中的效果了,我用的是真机测试,就不截图了。