在Maven项目中使用Android Support Library

使用命令行创建的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中的效果了,我用的是真机测试,就不截图了。


大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数Button. */ private Button mArgButton; /** * 全局Button. */ private Button mGlobleButton; /** * 退出Button. */ private Button mExitButton; public MainView(Context context){ super(context); setupViews(); } public MainView(Context context, AttributeSet attrs) { super(context, attrs); setupViews(); } private void setupViews(){ //获取View的上下文. mContext = getContext(); //这里将Context转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> 第4步:修改ApplicationDemoActivity.java,代码如下: package com.tutor.application; import android.app.Activity; import android.os.Bundle; public class ApplicationDemoActivity extends Activity { private static Context aContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainView mMainView = new MainView(this); setContentView(mMainView); aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值