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类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...
随机推荐
- linux磁盘问题排查
一.ls $>>ls /data* //查看有无input/output error报错 二.demsg $>>demsg|grep sd 问题盘结果: 三.iostat使用 ...
- HDU——2054A==B?
A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- java面试题之@Autowired和@Resource的区别
@Autowired和@Resource的区别: 1.都可以用来装配bean,都可以写在字段或者方法上: 2. @Autowired默认按类型装配,默认情况下必须要求依赖对象必须存在,如果允许为nul ...
- nginx 变量 + lua
nginx变量使用方法详解(8) nil.null与ngx.null 发现一个nginx LUA开发Web App的框架 nginx是个好东西, nginx的openrtsy发行版本更是个好东西. 今 ...
- 【CF20C】Dijkstra?(DIJKSTRA+HEAP)
没什么可以说的 做dijk+heap模板吧 以后考试时候看情况选择SFPA和DIJKSTRA ; ..]of longint; dis:..]of int64; a:..]of int64; b:.. ...
- 标准C程序设计七---37
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Larevel5.1 打印SQL语句
Larevel5.1 打印SQL语句 为了方便调试,开发时需要打印sql. 方法一(全局打开): SQL打印默认是关闭的, 需要在/vendor/illuminate/database/Connect ...
- iOS-开发者账号失效后是否还可以打包
参考链接:https://www.jianshu.com/p/601f596e8550
- git 中遇到的问题
刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...
- 使用Naive Bayes从个人广告中获取区域倾向
RSS源介绍:https://zhidao.baidu.com/question/2051890587299176627.html http://www.rssboard.org/rss-profil ...