在实际开发中,系统自带的wdigt常常们不能满足我们对控件的需求。我们就得自己开发自定义的控件。
在源码中我们需要像java类那样去继承(extends)我们要开发的控件父类。而在Android xml文件中的配置实现就需要
使用declare-styleable标签了
自定义控件的一般过程
1、在res/values/目录下创建一个attrs.xml,作为我们自定义控件的属性定义文件
2、编写内容:
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
这里我们定义了一个boolean类型的属性showText和emum类型的属性labelPosition
3、在 layout 文件夹中的定义文件中,在使用处引用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
4、在代码中获取 layout 文件中的值
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PieChart,
0, 0);
try {
mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
} finally {
a.recycle();
}
}
这里需要调用recycle()方法对资源进行回收
5、动态修改属性值
我们自定义的控件就可以从属性集中获取设置,这些属性值从配置中只能读取一次。要想实现动态访问和修改属性值,则需要向外部提供访问和修改的方法。如下:
public boolean isShowText() {
return mShowText;
}
public void setShowText(boolean showText) {
mShowText = showText;
invalidate();
requestLayout();
}
各种属性类型的自定义简介
一、reference:参考指定Theme中资源ID。
1.定义:
<declare-styleable name="My">
<attr name="label" format="reference" >
</declare-styleable>
2.使用:
<Buttonzkx:label="@string/label" >
二、Color:颜色
1.定义:
<declare-styleable name="My">
<attr name="textColor" format="color" />
</declare-styleable>
2.使用:
<Button zkx:textColor="#ff0000"/>
三、boolean:布尔值
1.定义:
<declare-styleable name="My">
<attr name="isVisible" format="boolean" />
</declare-styleable>
2.使用:
<Button zkx:isVisible="false"/>
四、dimension:尺寸值
1.定义:
<declare-styleable name="My">
<attr name="myWidth" format="dimension" />
</declare-styleable>
2.使用:
<Button zkx:myWidth="100dip"/>
五、float:浮点型
1.定义:
<declare-styleable name="My">
<attr name="fromAlpha" format="float" />
</declare-styleable>
2.使用:
<alpha zkx:fromAlpha="0.3"/>
六、integer:整型
1.定义:
<declare-styleable name="My">
<attr name="frameDuration" format="integer" />
</declare-styleable>
2.使用:
<animated-rotate zkx:framesCount="22"/>
七、string:字符串
1.定义:
<declare-styleable name="My">
<attr name="Name" format="string" />
</declare-styleable>
2.使用:
<rotate zkx:pivotX="200%"/>
八、fraction:百分数
1.定义:
<declare-styleable name="My">
<attr name="pivotX" format="fraction" />
</declare-styleable>
2.使用:
<rotate zkx:Name="My name is michael"/>
九、enum:枚举
1.定义:
<declare-styleable name="My">
<attr name="language">
<enum name="English" value="1"/>
</attr>
</declare-styleable>
2.使用:
<Button zkx:language="English"/>
十、flag:位或运算
1.定义:
<declare-styleable name="My">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="1" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
2.使用:
<activity android:windowSoftInputMode="stateUnspecified | adjustNothing">
属性定义时可以指定多种类型值:
<declare-styleable name = "名称">
<attr name="background" format="reference|color" />
</declare-styleable>
使用:
<ImageView android:background = "@drawable/图片ID|#00FF00"/>