設計自定義的控件對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官網上介紹的就是這種方式,簡單方便。