在android应用开发过程中,固定的属性可能还不能满足开发的需求,在一些特殊情况下,如一个视图是由几个控件组成的,而这个视图的重用性很大,为了开发的方便,就需要自定义一个视图,这时就运用到了自定义控件与属性。这几天在学习satellite_menu的demo时,看到里面也运用到了自定义属性,为此进一步探讨下这一方法的运用。
一、控件自定义属性介绍
首先这些属性所包括类型有:color , bool, dimension , float , integer , string , fraction , enum , flag .
以下示例中代码均在values/attrs.xml 中定义,而属性可随意命名
1. reference:参考某一资源ID
1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"/>
2. color:颜色值
1)属性定义:
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
2)属性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"/>
3.boolean:布尔值
1)属性定义:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"/>
4.dimension:尺寸值
1)属性定义:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"/>
5. float:浮点值
1)属性定义:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
2)属性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"/>
6.integer:整型值
1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
2)属性使用:
<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"
/>
7. string:字符串
1)属性定义:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
2)属性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"/>
8. fraction:百分数
1)属性定义:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
2)属性使用:
<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"/>
9. enum:枚举值
1)属性定义:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
2)属性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
</LinearLayout>
10. flag:位或运算
1)属性定义:
<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>
2)属性使用:
<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>
11.指定多种类型值的属性
1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"/>
二、属性的使用以及自定义控件的实现
* 说明在代码中注解
1)在res/values文件下定义一个attrs.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TestAttr">
<attr name="testTextSize" format="dimension"/>
<attr name="testColor" format="color"/>
</declare-styleable>
</resources>
2)在布局main.xml中使用该属性,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:testattr="http://schemas.android.com/apk/res/jp.com.attributecustomization"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<!-- 注:声明命名空间 xmlns:testattr="http://schemas.android.com/apk/res/jp.com.attributecustomization"
其中http://schemas.android.com/apk/res/为固定写法,其后追加包名 testattr为我们给自定义属性的别名引用,可以使用其他名称
-->
<jp.com.attributecustomization.ViewSubclass
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_centerInParent="true"
testattr:testTextSize="30dip"
testattr:testColor="#ff0000"
/>
</RelativeLayout>
3) 建立ViewSubclass类
package jp.com.attributecustomization;
import jp.com.attributecustomization.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
/**
* Demo描述: Android自定义属性的使用
*
* 注意事项:
* 1) 在main.xml中声明命名空间
* xmlns:testattr="http://schemas.android.com/apk/res/jp.com.attributecustomization"
* 其中http://schemas.android.com/apk/res/为固定写法,其后追加包名 testattr为我们给自定义属性的别名引用
* 2)getDimension(R.styleable.TestAttr_testTextSize, 20);
* 第二个参数意思是:假如在xml文件中没有为改属性设值则采用此值. 其余getXX()方法均类似
* 3)注意getColor()方法中第二个参数的取值,是一个颜色值,在这里很容易错误
*
*/
public class ViewSubclass extends View {
private Paint mPaint;
private float textSize;
private int textColor;
public ViewSubclass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ViewSubclass(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.TestAttr);
textSize = typedArray.getDimension(R.styleable.TestAttr_testTextSize,//引用自定义属性TestAttr_testTextSize,注意前面需要加TestAttr
20);
textColor = typedArray.getColor(R.styleable.TestAttr_testColor,//引用自定义属性TestAttr_testColor,注意前面需要加TestAttr
Color.BLACK);
System.out.println("textSize=" + textSize + ",textColor=" + textColor);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
// 切记recycle()
typedArray.recycle();
}
public ViewSubclass(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Style.FILL);
//画布显示“Test Attribute”字样
canvas.drawText("Test Attribute", 0, 80, mPaint);
}
}
4)建立AttributeSetActivity类
package jp.com.attributecustomization;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class AttributeSetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;//
}
}
效果图: