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类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...
随机推荐
- ccna 闫辉单臂路由 和 acl access control list
ccna 闫辉单臂路由 和 acl access control list 一单臂路由 当前园区网设计很少用到 成本低 小型的.局域网可用 二ACL acc ...
- CodeM初赛B轮
做什么啊,我这么菜,应该弃赛的 [编程|1500分] 子串 时间限制:3秒空间限制:32768K 题目描述 给出一个正整数n,我们把1..n在k进制下的表示连起来记为s(n,k),例如s(16,16) ...
- MFC拾遗
c++处理文件,对txt进行处理,学了那么久才发现我还不会这些操作,另外c++处理文本真是快得可怕啊 #include <iostream> #include <fstream> ...
- Eclipse调试程序及项目的导入导出
Eclipse调试程序 调试概述: ① 调试就是测试程序的方法,主要的目的就是解决程序的逻辑问题,流程是:发现问题.修改问题.正确执行; ② 以前我们可以使用System.out.printl ...
- 【Kubernetes】The connection to the server <master>:6443 was refused - did you specify the right host or port?
不知道怎么用着用着,使用kubectl 时报错,错误如下: root@R740--:~# kubectl get pod The connection to the server 107.105.13 ...
- java jstl标签
转自:http://blog.csdn.net/liushuijinger/article/details/9143793 JSTL(JSP Standard Tag Library ,JSP标准标签 ...
- BZOJ1189 [HNOI2007]紧急疏散evacuate 【二分 + 网络流】
题目 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以从 ...
- vue的过渡动画在除了chrome浏览器外的浏览器下不正常的问题
为过渡动画添加mode="out-in"在其它浏览器下面就能正常的使用了
- Tomcat和JVM的内存配置
原文路径:https://www.cnblogs.com/pangxiansheng/p/5378537.html JVM内存分配设置的参数有四个 -Xmx Java Heap最大值,默认值为物理内存 ...
- hdu 3501 容斥原理或欧拉函数
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...