Android提供了一個預(yù)建的部件,如Button, TextView, EditText, ListView, CheckBox, RadioButton, Gallery, Spinner, AutoCompleteTextView等可以直接使用在Android應(yīng)用程序開發(fā)中,但有可能還有一種情況,當不滿意現(xiàn)有可用的窗口小部件的功能。 Android 提供創(chuàng)建自定義組件功能,定制以滿足需求。
如果只需要進行小的調(diào)現(xiàn)有的小工具或布局,可以簡單的子類的小工具或布局和覆蓋它的方法,這將精確地控制屏幕元素的外觀和功能。
本教程介紹了如何創(chuàng)建自定義視圖,并利用它們在應(yīng)用程序,如下步驟。
最簡單的創(chuàng)建自定義的組件方法是擴展現(xiàn)有的widget類或子類,如果想擴展現(xiàn)有部件,如Button, TextView, EditText, ListView, CheckBox等,否則可以從android.view.View類開始擴展。
在其最簡單的形式,編寫構(gòu)造函數(shù)對應(yīng)的所有基類的構(gòu)造函數(shù)。例如,如果要擴展 TextView 創(chuàng)建DateView 以下三個構(gòu)造,創(chuàng)建DateView類:
public class DateView extends TextView { public DateView(Context context) { super(context); //--- Additional custom code -- } public DateView(Context context, AttributeSet attrs) { super(context, attrs); //--- Additional custom code -- } public DateView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //--- Additional custom code -- } }
TextView 的子類DateView已經(jīng)創(chuàng)建,所以可以獲得有關(guān)TextView 的所有屬性、方法和事件,能夠使用不需要任何進一步的實現(xiàn)。這里將實現(xiàn)額外的自定義功能在自己編寫的代碼,如下面的例子解釋。
如果要求執(zhí)行自定義繪圖/定制部件的尺寸,那么需要重寫 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 和 onDraw(Canvas canvas) 方法。如果不打算調(diào)整或變更內(nèi)置組件的形狀,那么并不需要使用這些方法在自定義組件。
布局管理報告部件的寬度和高度需要協(xié)調(diào) onMeasure() 方法,需要調(diào)用setMeasuredDimension(int width, int height),這種方法來報告尺寸大小。
可以執(zhí)行自定義繪圖里Canvas 的onDraw(Canvas canvas) 方法,其中android.graphis.Canvas其對應(yīng) Swing 是非常相似的,drawRect(), drawLine(), drawString(), drawBitmap() 等,可以用它來繪制組件。
完成了一個自定義組件的實現(xiàn)之后,通過擴大現(xiàn)有的部件,將能夠?qū)嵗@些自定義組件在應(yīng)用程序開發(fā)兩種方式:
這是非常相似的方式實例化自定義組件實例的方式,在活動類的內(nèi)置部件。例如,可以使用下面的代碼實例上面定義的自定義組件:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DateView dateView = new DateView(this); setContentView(dateView); }
查看這個例子來了解如何使用代碼里面活動實例化一個基本的Android自定義組件。
使用傳統(tǒng)布局XML文件實例的內(nèi)置部件,相同的概念適用于自定義部件,因此將能夠?qū)嵗远x組件布局XML文件,解釋如下。在com.yiibai.dateviewdemo包,已經(jīng)把所有的代碼相關(guān)DateView 和 DateView 類,已經(jīng)把自定義組件的完整的邏輯的Java類名。
<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" > <com.yiibai.dateviewdemo.DateView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="40sp" android:background="#000" /> </RelativeLayout>
要注意,在這里我們使用的所有 TextView 屬性以及自定義組件沒有任何變化。類似的方式能夠使用所有的事件、方法,以及DateView組件。
通過這個例子,了解如何使用布局XML文件實例化一個基本的Android自定義組件。
我們已經(jīng)看到可以如何擴展功能的內(nèi)置部件,但上面給出兩個例子中看到,擴展組件,可以利用它的父類的所有默認屬性。但考慮到一種情況,當想從頭開始創(chuàng)建自己的屬性。下面是一個簡單的程序創(chuàng)建和使用Android的自定義組件的新屬性。這里介紹三個屬性,并使用它們,如下所示:
<com.yiibai.dateviewdemo.DateView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="40sp" custom:delimiter="-" custom:fancyText="true" />
第一步,使用自定義的屬性在 res/values/ 目錄下創(chuàng)建新XML文件中定義attrs.xml??纯匆粋€例子文件 attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="DateView"> <attr name="delimiter" format="string"/> <attr name="fancyText" format="boolean"/> </declare-styleable> </resources>
這里 name=value 就是要使用的布局XML文件中并作為屬性,format=type 屬性的類型。
第二個步驟將是從布局XML文件中讀取這些屬性,并將其設(shè)置為組件。這個邏輯將獲得通過屬性集的構(gòu)造函數(shù),因為這是包含XML屬性。要讀取XML中的值,首先需要從AttributeSet創(chuàng)建一個TypedArray,然后用它來讀取和設(shè)置值,如下面的示例代碼所示:
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateView); final int N = a.getIndexCount(); for (int i = 0; i < N; ++i) { int attr = a.getIndex(i); switch (attr) { case R.styleable.DateView_delimiter: String delimiter = a.getString(attr);上一篇:Android內(nèi)部存儲下一篇:Android CheckBox