Android学习笔记(十五)自定义控件(declare-styleable 的使用)
来源:互联网 发布:asd a0421 ab软件 编辑:程序博客网 时间:2024/05/29 07:22
在实际开发中,系统自带的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"/>
参考:
http://blog.csdn.net/congqingbin/article/details/7869730
http://www.xiaoyunduo.org/article/83/
http://developer.android.com/training/custom-views/create-view.html
0 0
- Android学习笔记(十五)自定义控件(declare-styleable 的使用)
- 【Android】declare-styleable:自定义控件的属性
- Android declare-styleable:自定义控件的属性
- 自定义控件declare-styleable的使用
- Android自定义控件使用declare-styleable进行属性配置
- Android自定义控件使用declare-styleable进行属性配置
- declare-styleable:自定义控件的属性 (笔记)
- declare-styleable:自定义控件的属性的使用
- declare-styleable自定义控件
- [Android开发] declare-styleable:自定义控件的属性
- [Android xml]自定义控件的属性-declare-styleable
- Android中自定义控件的declare-styleable属性重用方案
- android自定义控件 ( TypedArray 、declare-styleable)
- Android declare-styleable自定义控件属性
- Android declare-styleable自定义控件属性注意事项
- Android declare-styleable自定义控件属性
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Mysql EF Database First 生成标准C#规范的类文件结构
- DP 之 poj 3280
- 翻纸牌游戏 贪心
- 黑马程序员——【Java】【高新技术】自定义动态代理,过程、DEMO
- 黑马程序员——【Java】【高新技术】代理作用、原理、AOP
- Android学习笔记(十五)自定义控件(declare-styleable 的使用)
- android中webview/webviewclient截获401响应
- DP 之 poj 2955
- GitHub入门之一:使用github下载项目
- 黑马程序员 自学0304C#编程基础之类型的转换与转型
- 初识UML
- 黑马程序员——【Java】【高新技术】类加载器的高级问题分析
- sdoj 小鑫的城堡 并查集
- 005_005 Python 根据字符串中的数字排序,如f10应该在f2后面