一、序言
控件有很多属性,如 android:id、android:layout_width,这些属于系统自带的属性,我们还可以使用自定义的属性,这就要说到attrs.xml文件的创建与使用;
二、attrs.xml文件的使用
(1)在values文件夹下,新建一个attrs.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="tColor" format="color" />
<attr name="tSize" format="dimension" />
</declare-styleable>
</resources>
其中,<declare-styleable name="CustomView">表明样式名称为CustomView,下面包含了两个自定义属性tColor和tSize,其中tColor是颜色(color)类的属性,tSize是尺寸(dimension)类的属性;
(2)定义CustomView类,CustomView.java的构造函数如下:
首先从R.styleable.CustomView获得了TypedArray变量,再用getColor(),getDimension()等方法获取相应的属性值,属性格式为“样式名_属性名”,属性后面的参数是默认值。获得属性值以后,就可以应用这些属性值。recycle()方法用于返回信号给资源(不懂什么意思)。
// 构造函数
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// 获得TypedArray
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
// 获得attrs.xml里面的属性值,格式为:名称_属性名,后面是默认值
int tColor = a.getColor(R.styleable.CustomView_tColor, Color.GREEN);
float tSize = a.getDimension(R.styleable.CustomView_tSize, 35);
p.setColor(tColor);
p.setTextSize(tSize);
// 返回一个绑定资源结束的信号给资源
a.recycle();
}
(3)编辑使用该view的主窗体的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:test="http://schemas.android.com/apk/res/com.zz.customview"
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.zz.customview.CustomView
android:id="@+id/cusView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
test:tColor="#00FFFF"
test:tSize="30dp"
>
</com.zz.customview.CustomView>
</RelativeLayout>
注意,主标签中要添加xmlns:test="http://schemas.android.com/apk/res/com.zz.customview"这一条属性,其中com.zz.customview是包名,这样就可以使用自定义的view了,在控件属性中也可以增加test:tColor和test:tSize这两个属性;
三、attrs.xml详解
本节我们主要学习attrs.xml文件中的各属性的含义与用法,即不同的format属性值;
3.1 reference:参考某一资源ID
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
<!-- 使用实例 -->
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"/>
3.2 color:颜色值
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
<!-- 使用实例 -->
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"/>
3.3 boolean:布尔值
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
<!-- 使用实例 -->
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"/>
3.4 dimension:尺寸值
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
<!-- 使用实例 -->
<com.lizi.newset.CustomView.attrs.MyView
android:id="@+id/myView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
test:textSize="50px"
test:textColor="#ff00ff"/>
3.5 float:浮点值
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
<!-- 使用实例 -->
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"/>
3.6 string:字符串
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
<!-- 使用实例 -->
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"/>
3.7 integer:整型值 || fraction:百分数
<!-- attrs.xml中的属性定义 -->
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" format = "fraction"/>
<attr name = "pivotY" format = "fraction"/>
<attr name = "drawable" />
</declare-styleable>
<!-- 使用实例 -->
<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"/>
3.8 enum:枚举值
<!-- attrs.xml中属性的定义 -->
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
<!-- 使用实例 -->
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
</LinearLayout>
3.9 flag 位或运算
<!-- attrs.xml中属性的定义 -->
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
<!-- 使用实例 -->
<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3.10 属性可以同时定义多种值
<!-- attrs.xml中属性的定义 -->
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
<!-- 使用实例 -->
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"/>