@H_403_1@转载:http://www.cnblogs.com/hibraincol/archive/2011/10/27/2227149.html
@H_403_1@今天在看android froyo的launcher2 源码的时候,在launcher.xml中看到有这么一段代码:
@H_403_1@注意到其中的两处:
@H_403_1@解释如下:
@H_403_1@属性relation有4种可选值:icon_left,icon_right,icon_above,icon_below.
@H_403_1@属性icon的可选值为引用: 例如:"@/drawbable/icon".
@H_403_1@属性text的可选值为string, 例如: "Hello world",也可是string的引用"@string/hello".
@H_403_1@属性text_size的可选值为尺寸大小,例如:20sp、18dip、20px等.
@H_403_1@属性text_color的可选值为整数,例如:"0xfffffff", 也可以是color的引用"@color/white".
@H_403_1@
@H_403_1@2. 定义一个能够处理这些属性值的view或者layout类
<
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"
>
|
@H_403_1@xmlns:launcher=”http://schemas.android.com/apk/res/com.android.launcher”@H_403_1@和
@H_403_1@launcher:defaultScreen="2"@H_403_1@可以看出在这个布局文件中,使用了自定义属性。 @H_403_1@ @H_403_1@以前没遇到过,既然这里碰到了,就顺便学习下,下面就写个简单的示例,权当学习笔记,便于以后查阅。 @H_403_1@1. 定义一些自定义属性 @H_403_1@建立一个属性xml文件: values/attrs.xml,内容如下:
resources
>
<!-- the relation between the icon and text. -->
attr
name
"relation"
>
enum
"icon_left"
value
"0"
/>
"icon_right"
"1"
/>
"icon_above"
/>
"icon_below"
"3"
/>
</
attr
>
skip
/>
declare-styleable
"IconText"
>
/>
"icon"
format
"reference"
/>
"text"
"string"
/>
"text_size"
"dimension"
/>
"text_color"
"integer"
/>
"space"
/>
declare-styleable
>
>
import
android.content.Context;
android.content.res.TypedArray;
android.util.AttributeSet;
android.util.Log;
android.widget.ImageView;
android.widget.LinearLayout;
android.widget.TextView;
public
class
IconTextView
extends
LinearLayout {
private
final
static
String TAG =
"IconTextView"
;
int
ICON_LEFT =
0
;
ICON_RIGHT =
1
;
ICON_ABOVE =
2
;
ICON_BELOW =
3
;
private
TextView mTextView;
ImageView mImageView;
mRelation = ICON_LEFT;
String mText =
""
;
mIconId;
float
mTextSize;
mSpace;
public
IconTextView(Context context,AttributeSet attrs){
super
(context,attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.IconText);
mRelation = a.getInt(R.styleable.IconText_relation,ICON_LEFT);
Log.d(TAG,
"mRelation: "
+mRelation);
mText = a.getString(R.styleable.IconText_text);
"mText: "
+mText);
mTextSize = a.getDimensionPixelSize(R.styleable.IconText_text_size,
12
);
"mTextSize: "
+mTextSize);
mSpace = a.getDimensionPixelSize(R.styleable.IconText_space,0)!important">5
);
"mSpace: "
+mSpace);
mIconId = a.getResourceId(R.styleable.IconText_icon,R.drawable.icon);
"mIconId: "
+mIconId);
a.recycle();
mTextView =
new
TextView(context);
mTextView.setText(mText);
mTextView.setTextSize(mTextSize);
mImageView =
ImageView(context);
mImageView.setImageResource(mIconId);
left =
;
top =
;
right =
;
bottom =
;
orientation = HORIZONTAL;
textViewIndex =
;
switch
(mRelation){
case
ICON_ABOVE:
orientation = VERTICAL;
bottom = mSpace;
;
break
;
ICON_BELOW:
orientation = VERTICAL;
top = mSpace;
;
ICON_LEFT:
right = mSpace;
;
;
ICON_RIGHT:
left = mSpace;
;
}
this
.setOrientation(orientation);
.addView(mImageView);
mImageView.setPadding(left,top,right,bottom);
.addView(mTextView,textViewIndex);
}
}