设计自定义的控件对android开发人员来讲,是屡见不鲜了,可是屡次作项目的经验证实了一个道理,自定义的控件,能够在其余项目中,屡次使用,因此接下来咱们来介绍2种经常使用的打包方式,并讨论他们的利于病。 java
咱们能够假设想要自定义一个改变文字显示的button(纯属假设,这样简单的功能其实也用不着自定义) android
首先写好布局文件mybutton.xml 算法
<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 ide
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); } }
1右击项目,选择export,选择java中的jar如图 布局
图 01 ui
勾选出自定义控件相关文件,如图02 this
图02 google
选好后选择finish就完成了导出, spa
在使用的时候,将要jar包放到新的项目中的libs文件中(通常会自动引入,若是没有自动引入能够右击该包而后选择Build path,再选择add to build path就能够了), 设计
图 03
在新项目布局文件中加入自定义控件;
布局文件main.xml
<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>
缘由是jar打包后放在drawable中的图片资源没法找到了,,,解决的方式就是将图片等资源文件放在assets文件夹中,再在java代码中映射出来找到资源,这就增长的工做负担,因此一般在使用自定义控件的时候不用这种方式,而是采用下面将要介绍的第二种方式,
优点:jar打包方式,能够用在不使用图片资源的项目中,封装算法等特别方便,便于其余人使用,
劣势:失去了索引,没法使用drawable中的图片资源,封装后的代码修改起来麻烦
在设计自定义控件的时候,在新建时能够选择将项目做为library(如图04),也能够以后进行设置
图 04
右击项目。选择android,再勾选出Is Library便可(如图05);
图 05
这样即可以使用drawable中的图片资源了,不过须要特别注意的是,自定义控件中的图片名称与新项目中的图片资源不能重名,须要本身检查一下(自定义控件中的默认图标ic_launcher.png须要删除掉,否则会报错,经试验xml文件能够重名)
优点:可使用drawable中的图片资源了,如今google官网上介绍的就是这种方式,简单方便。