以launcher為例說明自訂控制項的屬性:
1、 在layout裡面定義控制項,如:
<com.junction.launcher.DragLayer
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"
android:id="@+id/drag_layer" android:layout_width="match_parent" android:layout_height="match_parent">
<com.android.launcher.Workspace
android:id="@+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:defaultScreen="1">
<include android:id="@+id/cell1" layout="@layout/workspace_screen" />
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />
<include android:id="@+id/cell3" layout="@layout/workspace_screen" />
</com.android.launcher.Workspace>
2、 該控制項workspace下面有一個屬性是launcher:defaultScreen,這屬性不是ViewGroup(workspace類是繼承於ViewGroup)所定義的屬性,那麼我們可以在\res\values裡面的attrs.xml裡面定義
具體的定義方法如下:
<resources>
<declare-styleable name="Workspace">
<attr name="defaultScreen" format="integer" />
</declare-styleable>
</resources>
引用方法是 先申明xmlns:launcher="http://schemas.android.com/apk/res/com.junction.launcher"(R.java),這樣就可以使用launcher:defaultScreen。
3、 這樣我們在Workspace的類裡面就可以去除在layout裡面對該屬性的賦值,方法如下:
Int mDefaultScreen;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);
mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);
a.recycle();
++++++++++++++++++++++++++++++
自己案例:可看一個gifPlayer的demo程式。
其他案例:http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx
++++++++++++++++++++++++++++++
轉載自:http://blog.csdn.net/tinafhx/archive/2010/02/05/5290878.aspx