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类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...
随机推荐
- php 操作excel
<?php $dir=dirname(__FILE__);//查找当前脚本所在路径 require $dir."/db.php";//引入mysql操作类文件 require ...
- [git 学习篇]git管理的是修改,并非文件
你会问,什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. 为什么说Git管理的 ...
- JAVA调用oracle存储过程实例
1.创建添加存储过程 CREATEORREPLACEPROCEDURE stu_proc(v_id INNUMBER, v_name INVARCHAR2, v_age INNUMBER) AS BE ...
- java中equals和hashCode方法随笔二
前几天看了篇关于java中equals和hashCode方法的解析 1.Object类中的equals方法和hashCode方法. Object类中的equals和hashCode方法简单明了,所有的 ...
- HDU——2064汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- [LOJ#531]「LibreOJ β Round #5」游戏
[LOJ#531]「LibreOJ β Round #5」游戏 试题描述 LCR 三分钟就解决了问题,她自信地输入了结果-- > -- 正在检查程序 -- > -- 检查通过,正在评估智商 ...
- 都系坤坤-微信助手V 0.1,解放双手发信息
端午节刚过,相信大家在端午节都收到不少微信祝福信息,有复制长篇大论的祝福语群发的,有人工手打的简单祝福群发,我更喜欢人工手打带上称呼的祝福信息,这样看起来更加亲切. 那么问题来了,当你的通讯录里好友多 ...
- 【CCF】无线网络 搜索+思维
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- iOS-ASIHTTPRequest缓存机制
第三方网络请求库 * 我们在对网络请求的时候,可以使用系统为我们提供的NSURLRequest和NSURLConnection,它基本能实现我们的基本功能. * 但是有时我们使用第三方封装的库,可以轻 ...
- Failed to apply plugin [id 'com.gradle.build-scan']
把spring源码clone下来之后,使用gradle编译不通过,异常日志如下: FAILURE: Build failed with an exception. * Where: Build fil ...