荐 android 如何打包自定义控件(转)

 

设计自定义的控件对android开发人员来说,是家常便饭了,但是多次做项目的经验证明了一个道理,自定义的控件,可以在其他项目中,多次使用,所以接下来我们来介绍2种常用的打包方式,并讨论他们的利于病。

我们可以假设想要自定义一个改变文字显示的button(纯属假设,这样简单的功能其实也用不着自定义)

首先写好布局文件mybutton.xml

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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingBottom="5dip"
        android:paddingLeft="40dip"
        android:layout_centerVertical="true"
        android:paddingTop="5dip"
        android:src="@drawable/button" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_centerVertical="true"
        android:text="确定"
        android:layout_toRightOf="@id/imageView1"
        android:textColor="#000000" />
 
</RelativeLayout>

再完成控制类MyProgressBar.java

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.swastika.mywidget;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyProgressBar extends LinearLayout {
 
    private ImageView imageView;
    private TextView  textView;
    boolean flag = true;
     
    public MyProgressBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.mybutton, this);
        imageView=(ImageView) findViewById(R.id.imageView1);
        textView=(TextView)findViewById(R.id.textView1);
        textView.setOnClickListener(new OnClickListener() {
             
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(flag){
                    textView.setText("取消");
                    flag = false;
                }
                else {
                    textView.setText("确定");
                    flag = true;
                }
            }
        });
    }
     
    /**
     * 设置图片资源
     */ 
    public void setImageResource(int resId) { 
        imageView.setImageResource(resId); 
    
   
    /**
     * 设置显示的文字
     */ 
    public void setTextViewText(String text) { 
        textView.setText(text); 
    
 
     
}

这样只要引入控件com.swastika.mywidget.MyProgressBar,便可以实现功能了。下面就要介绍打包方式了。

方式一:将项目打包成jar包

1右击项目,选择export,选择java中的jar如图

图 01

勾选出自定义控件相关文件,如图02

图02

选好后选择finish就完成了导出,

在使用的时候,将要jar包放到新的项目中的libs文件中(一般会自动引入,如果没有自动引入可以右击该包然后选择Build path,再选择add to build path就可以了),

图 03

在新项目布局文件中加入自定义控件;

布局文件main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
     
        <com.swastika.mywidget.MyProgressBar
        android:id="@+id/imgBtn0" 
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        />
 
</RelativeLayout>

不过运行的时候会发生错误java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtest/com.example.androidtest.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class com.swastika.mywidget.MyProgressBar,

原因是jar打包后放在drawable中的图片资源无法找到了,,,解决的方式就是将图片等资源文件放在assets文件夹中,再在java代码中映射出来找到资源,这就增加的工作负担,所以通常在使用自定义控件的时候不用这种方式,而是采用下面将要介绍的第二种方式,

优势:jar打包方式,可以用在不使用图片资源的项目中,封装算法等特别方便,便于其他人使用,

劣势:失去了索引,无法使用drawable中的图片资源,封装后的代码修改起来麻烦

方式二:项目作为一个library

在设计自定义控件的时候,在新建时可以选择将项目作为library(如图04),也可以之后进行设置

图 04

右击项目。选择android,再勾选出Is Library即可(如图05);

图 05

这样便可以使用drawable中的图片资源了,不过需要特别注意的是,自定义控件中的图片名称与新项目中的图片资源不能重名,需要自己检查一下(自定义控件中的默认图标ic_launcher.png需要删除掉,不然会报错,经试验xml文件可以重名)

优势:可以使用drawable中的图片资源了,现在google官网上介绍的就是这种方式,简单方便。

荐 android 如何打包自定义控件(转)的更多相关文章

  1. 更便捷的Android多渠道打包方式

    本文先回顾了以往流行的多渠道打包方式,随后引入的mcxiaoke的packer-ng-plugin项目,介绍该项目在实际应用(配合友盟统计)中如何解决更方便的Android多渠道打包问题 多渠道打包方 ...

  2. eclipse 和 android studio 打包签名apk问题

    首先,我使用eclipse打包了一个签名apk 然后,我使用同一个签名文件在android studio 打包同一个项目 接下来,首先安装eclipse 打包的apk,然后安装android stud ...

  3. Android Studio打包未签名包

    Android Studio打包未签名包 好久没有写技术博客了,真有点懈怠了,作为35岁的程序员,转行重新捡起这些知识,还是挺犹豫纠结的,不过没啥其它办法,一点一滴开始吧,今天这开篇就小结点前几天工作 ...

  4. Android 类库打包、发布方法

    开发Android应用的时候,对于可用于多个应用的公用的部分,或是打算发布给第三方进行应用集成的部分,要把这部分打包成类库怎么做呢? Android应用使用ADT打包成apk,apk中包含了运行程序所 ...

  5. android studio开发工具的android library打包文件(.aar)本地引用

    by 蔡建良 2014-5-13 关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 a ...

  6. 【转】android Apk打包过程概述_android是如何打包apk的

    最近看了老罗分析android资源管理和apk打包流程的博客,参考其他一些资料,做了一下整理,脱离繁琐的打包细节和数据结构,从整体上概述了apk打包的整个流程.   流程概述: 1.打包资源文件,生成 ...

  7. Android混淆打包配置总结

    Android打包失败出现Proguard returned with error code 1. See console的错误 这个问题是由于代码混淆引起的,找不到引用包. 只需在你的proguar ...

  8. Android Studio 打包及引用 aar

    Android Studio 打包及引用 aar 1. 简述 在比较大的 Android 项目的开发中,我们经常会遇到工程.jar 包等等之间相互引用的方式.一般我们通过在 gradle 文件中配置依 ...

  9. Android应用打包安装过程具体解释

    Android应用打包安装过程(Run as Android Application ): 1,过程:Android Project --> Compilation and Packaging ...

随机推荐

  1. Android Studio 项目结构

    在Android Studio中创建项目后,会出现 activity_main.xml 和 MainActivity.java 两个文件 其中  Java文件定义了一个Activity,当应用运行时, ...

  2. Mybatis插件原理和PageHelper结合实战分页插件(七)

    今天和大家分享下mybatis的一个分页插件PageHelper,在讲解PageHelper之前我们需要先了解下mybatis的插件原理.PageHelper 的官方网站:https://github ...

  3. 获取机器网卡的物理(MAC)地址

    <?php  /**   * 获取机器网卡的物理(MAC)地址* 目前支持WIN/LINUX系统   * 编辑: www.jbxue.com**/  class MacAddInfo {     ...

  4. JavaScript高级程序设计:第八章

    1.window对象——BOM的核心 BOM的核心对象时window,它表示浏览器的一个实例.在浏览器中,window对象有双重角色,它既是通过javascript访问浏览器窗口的一个接口,又是ECM ...

  5. JavaScript高级程序设计:第六章

    第六章 面向对象的程序设计 一.理解对象 1.属性类型: ECMAScript中有两种属性:数据属性和访问器属性. (1)数据属性: 数据属性包含一个数据值的位置.在这个位置可以读取和写入值.数据属性 ...

  6. hdu_2665_Kth number(主席树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2665 题意:给你一个区间,让你找这个区间第K大的数 题解:主席树模版题,也可以用划分树 #includ ...

  7. bingo 跨action异步获取参数

    html(定时器模拟异步) <script> setTimeout(function(){ window.teacInfo = {a:1,b:2}; },2000);</script ...

  8. PHP问答题大全

    答案在题目后面,文字与背景同色,连续单机三次鼠标一行出答案哦: 1.PHP有几种原始数据类型,分别是什么?答:八种,分别是:int,float,string,bool,array,object,res ...

  9. Qt之日志输出窗口

    来源:http://blog.sina.com.cn/s/blog_a6fb6cc90101guz0.html 继上节所讲,Qt可以很容易的将一些日志信息保存到文件中,那么日志信息如何输出到窗口呢? ...

  10. 移植Iperf到android 用来学习linux移植到安卓的例子

    Iperf移植记录 1.生成arm编译需要的头文件config.h ./configure --host=arm如果需要make clean make distclean2.增加Android.mk文 ...