Android自己定义控件
今天我们来讲一下 Android中自己定义控件的介绍,在Android中, 我们一般写xml都是用的是单个的控件来完毕的 ,但是。往往在一些项目中。单个控件有时是满足不了的。故此我们能够自己定义控件 ,用自己定义控件的优点是 一方面是更加灵活,还有一方面在大数据量的情况下自己定义控件的效率比写布局文件更高 。其它地方要用此控件 仅仅须要引用控件就能够了。
以下来写一个自己定义控件的和它须要注意的地方:
1.建立一个xml文件 写你想要自己定义的布局
2.在res/values 创建一个attrs.xml 如以下的结构
<resources>
<declare-styleable name="myshow">
<attr name="tvTitle" format="string"/>
<attr name="titleMinLength" format="integer"/>
<attr name="tvHint" format="string"/>
<attr name="title" format="color"/>
<attr name="tvValue" format="string"/>
</declare-styleable>
</resources>
具体介绍各个attr标签中的format属性的类型和多指定类型
《1》"reference" //引用 參考某一资源ID
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
使用:
<ImageView
android:layout_width = "42dp"
android:layout_height = "42dp"
android:background = "@drawable/图片ID"
/>
《2》"color" //颜色
<declare-styleable name = "名称">
<attr name = "textcolor" format = "color" />
</declare-styleable>
使用:
<TextView
android:layout_width = "42dp"
android:layout_height = "42dp"
android:textcolor = "#ffffff"
/>
《3》 "boolean" //布尔值
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
使用:
<Button
android:layout_width = "42dp"
android:layout_height = "42dp"
android:focusable = "true"
/>
《4》"dimension" //尺寸值
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
使用:
<Button
android:layout_width = "42dp"
android:layout_height = "42dp"
/>
《5》"float" //浮点值
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
/>
《6》"integer" //整型值
<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>
使用:
<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 = "AlphaAnimation">
<attr name = "strName format = "string" />
<attr name = "strTitle" format = "string" />
</declare-styleable>
使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO"
/>
《8》"fraction" //百分数,比方200%
<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>
使用:
<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" //枚举值
<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\horizontal"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
《10》
注意:属性定义时能够指定多个类型值
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#ffffff"
/>
3.定义一个自己定义的类 继承一个View 继承哪一个View 依据你实际情况来决定
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGluNzI0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
4.引用自己定义的类到你须要的xml中
<1>增加前 必须引用他当前的位置路径 如:
xmlns:fx="http://schemas.android.com/apk/res/fx.com.bitmapsolution"
<2>引用详细的控件和获取控件中的某个属性
<fx.com.bitmapsolution.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
fx:tvTitle="@string/title3"
fx:titleMinLength="4"
android:layout_marginTop="5dp"
/>
到此 即完毕了一个较简单自己定义控件
复杂点自己定义控件 有时间再写一个 哈哈哈~~
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGluNzI0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Android自己定义控件的更多相关文章
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
- android 自己定义控件
Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...
- Android自己定义控件皮肤
Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...
- android 自己定义控件属性(TypedArray以及attrs解释)
近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这 ...
- Android自己定义控件之应用程序首页轮播图
如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...
- Android自己定义控件(状态提示图表)
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...
- Android自己定义控件系列二:自己定义开关button(一)
这一次我们将会实现一个完整纯粹的自己定义控件,而不是像之前的组合控件一样.拿系统的控件来实现.计划分为三部分:自己定义控件的基本部分,自己定义控件的触摸事件的处理和自己定义控件的自己定义属性: 以下就 ...
- Android 自己定义控件开发入门(二)
上一次我们讲了一堆实现自己定义控件的理论基础.列举了View类一些能够重写的方法,我们对这些方法的重写是我们继承View类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...
随机推荐
- 使用PHPExcel单元格样式添加不上的原因
按照文档上的样式设置方法设置样式,一直不成功.后来发现是得要在创建了sheet,并选择某一个sheet作为活动sheet后再设置才可以.这里记录一下.
- 【bzoj3680】吊打XXX 随机化
题目描述 gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty.gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了.蒟蒻们将n个gty吊在n根绳子上,每根绳子穿过天台的一个洞.这n根绳子有 ...
- 【bzoj3166】[Heoi2013]Alo 可持久化Trie树+STL-set
题目描述 Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG ,如名字所见,到处充满了数学的谜题.现在你拥有n颗宝石,每颗宝石 ...
- 性能学习之六---socket接口测试
socket协议较底层,所以是一个万能协议.socket发的是数据包,所以较难看懂. 下面我们来讲解socket接口测试. 大致思路为:新建sever端和client端---建立连接---发送数据 一 ...
- 免安装版MySql安装与配置
1:在MySql官网下载免安装版 http://downloads.mysql.com/archives/community/ 下载链接 http://downloads.mysql.com/arch ...
- 刷题总结——book of evil(codefoeces 337D)
题目: description Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This ar ...
- jsp生成war
安装java 环境,进入jsp所在目录,使用如下命令可将当前目录中所有文件打成到xss.war包中,正常的war包中还包含另外两个文件meta-inf,web-inf,在生成的时候,需要把这两个文件加 ...
- 【CF733F】Drivers Dissatisfaction(最小瓶颈生成树,倍增)
题意:给出一个图,每条边有权值和花费c,每次花费c能使的权值-1.给出一个预算,求减完权值后的一个最小生成树. 思路:感谢CC大神 有这样一个结论:最佳方案里必定存在一种,预算全部花费全部分配在一条边 ...
- 如何用github展示前端页面
如何在github上展示你的前端页面 参考:https://luozhihao.github.io/demo/ 感谢作者 1.New reposipory 2.进入你本机目录 我是在d:vuedemo ...
- [LeetCode] Sort Colors 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...