转载:http://www.cnblogs.com/hibraincol/archive/2011/10/27/2227149.html
今天在看android froyo的launcher2 源码的时候,在launcher.xml中看到有这么一段代码:
<
com.android.launcher2.DragLayer
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:launcher
"http://schemas.android.com/apk/res/com.android.launcher"
android:id
"@+id/drag_layer"
android:layout_width
"match_parent"
android:layout_height
"match_parent"
>
include
layout
"@layout/all_apps"
/>
<!-- The workspace contains 3 screens of cells -->
com.android.launcher2.Workspace
"@+id/workspace"
"match_parent"
"match_parent"
android:scrollbars
"horizontal"
android:fadeScrollbars
"true"
launcher:defaultScreen
"2"
>
|
注意到其中的两处:
xmlns:launcher=”http://schemas.android.com/apk/res/com.android.launcher”
和
launcher:defaultScreen="2"
以前没遇到过,既然这里碰到了,就顺便学习下,下面就写个简单的示例,权当学习笔记,便于以后查阅。
建立一个属性xml文件: values/attrs.xml,内容如下:
>
name
"relation"
>
"icon_left"
value
"0"
/>
"1"
/>
/>
"3"
/>
attr
>
/>
"IconText"
>
format
"reference"
/>
"string"
/>
"dimension"
/>
"integer"
/>
/>
>
解释如下:
属性relation有4种可选值:icon_left,icon_right,icon_above,icon_below.
属性icon的可选值为引用: 例如:"@/drawbable/icon".
属性text的可选值为string, 例如: "Hello world",也可是string的引用"@string/hello".
属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.
属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white".
2. 定义一个能够处理这些属性值的view或者layout类
android.content.Context;
class
IconTextView
extends
LinearLayout {
private
final
static
String TAG =
"IconTextView"
;
ICON_LEFT =
0
;
1
;
2
;
3
;
TextView mTextView;
""
;
mTextSize;
IconTextView(Context context,AttributeSet attrs){
(context,attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.IconText);
"mRelation: "
+mRelation);
+mText);
12
);
+mTextSize);
);
+mSpace);
+mIconId);
new
TextView(context);
ImageView(context);
;
;
;
;
;
(mRelation){
ICON_ABOVE:
;
.setOrientation(orientation);
}