android自定义控件
来源:互联网 发布:高新技术软件产业园 编辑:程序博客网 时间:2024/06/05 05:42
android中自定义控件是很常见的,很多应用都会用到。这里总结一下。
1、首先建一个类继承布局文件(可以是Relativelayout、Linearlayout…)或View。重写构造函数。
2、在values目录下编写attrs.xml属性文件,里面写自定义控件用到的属性。
3、使用方法:在布局文件中xmlns:custom(名字无所谓)=”http://schemas.android.com/apk/res/项目包名”,这样就可以在控件中使用这些属性。
上面是大概步骤,下面具体细节
1、先说说属性:
在attrs.xml中写需要的属性:
declare-styleable name:自定义属性文件的名字,在自定义控件类中调用
attr name:属性名 format:是属性的数据类型
format属性多很多种类下面说明:
1、reference:资源ID
属性定义:<declare-styleable name = "名称"> <attr name = "background" format = "reference" /></declare-styleable>属性使用: <ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID"/>
2、 color:颜色值。
属性定义:<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable>属性使用: <TextView android:layout_width = "42dip" android:layout_height = "42dip" android:textColor = "#00FF00" />
3、boolean:布尔值
属性定义:<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /></declare-styleable>属性使用: <Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true" />
4、dimension:尺寸
属性定义:<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /></declare-styleable>属性使用: <Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true" />
5、float:浮点值
属性定义:<declare-styleable name = "名称"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /></declare-styleable>属性使用: <alpha android:fromAlpha = "1.0" android:toAlpha = "0.7" />
6、 integer:整型值
属性定义:<declare-styleable name = "名称"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <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" />
7、 string:字符串
属性定义:<declare-styleable name = "名称"> <attr name = "apiKey" format = "string" /></declare-styleable>属性使用: <animated-rotate android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
8、 fraction:百分数
属性定义:<declare-styleable name = "名称"> <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>属性使用: <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>
- 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> 注意: 属性定义时可以指定多种类型值。 (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 0
- [Android自定义控件] Android自定义控件
- Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- [Android自定义控件] Android自定义控件
- Android 自定义控件 单页翻书控件
- android虚线控件---自定义控件
- android自定义控件实例 --控件
- Android自定义控件--组合控件
- android 控件 自定义组合控件
- 【android自定义控件】ProgressBar自定义
- Android自定义控件 自定义属性
- Android自定义控件 -- 自定义View
- Android自定义控件 -- 自定义ViewGroup
- Android 自定义控件 自定义标题栏
- Android 自定义控件-自定义进度条。
- cookie扫盲
- 解决android studio Error:SSL peer shut down incorrectly
- 【CI学习笔记】简单的session实例--添加与读取session
- 使用XMLHttpRequest 实现http 终止请求和超时
- K近邻算法
- android自定义控件
- Java经典设计模式之七大结构型模式
- CentOS No package nginx available.
- 关于Class.getResource和ClassLoader.getResource的路径问题
- ReentrantLock详解
- 331. Verify Preorder Serialization of a Binary Tree | Java最短代码实现
- apache shiro集群实现(一) session共享
- try/catch、throws和throw
- vb作业