今天我们来讲一下 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自己定义控件的更多相关文章

  1. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  2. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  3. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  4. Android自己定义控件皮肤

    Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...

  5. android 自己定义控件属性(TypedArray以及attrs解释)

    近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这 ...

  6. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  7. Android自己定义控件(状态提示图表)

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...

  8. Android自己定义控件系列二:自己定义开关button(一)

    这一次我们将会实现一个完整纯粹的自己定义控件,而不是像之前的组合控件一样.拿系统的控件来实现.计划分为三部分:自己定义控件的基本部分,自己定义控件的触摸事件的处理和自己定义控件的自己定义属性: 以下就 ...

  9. Android 自己定义控件开发入门(二)

    上一次我们讲了一堆实现自己定义控件的理论基础.列举了View类一些能够重写的方法,我们对这些方法的重写是我们继承View类来派生自己定义控件的关键 我通过一个最简单的样例给大家展示了这一个过程,不管是 ...

随机推荐

  1. 开发者工具删除元素Delete Element

    开发者工具有个很好用的功能,通过删除元素,可以查看页面哪些元素比较特殊,同时也可以排除干扰.

  2. Thinkphp5.1源码阅读

    主要流程 1 \public\index.php require __DIR__ . '/../thinkphp/start.php'; \thinkphp\start.php require __D ...

  3. JDBC 学习笔记(五)—— Statement

    JDBC 使用 Statement 作为 SQL 语句的执行器. Statement 通过 Connection.createStatement() 方法创建,一共支持以下6种方式执行 SQL 语句: ...

  4. Welcome-to-Swift-12附属脚本(Subscripts)

    附属脚本 可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,可以认为是访问对象.集合或序列的快捷方式,不需要再调用实例的特定的赋值和访问方法.举例来说 ...

  5. 刷题总结——湫湫系列故事——设计风景线(hdu4514 并差集判环+树的直径)

    题目:   随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.   现在已经勘探 ...

  6. P1875 佳佳的魔法药水 (最短路,DP)

    题目链接 Solution 好题. 一开始一直在想怎么蛇皮建图,但是发现一直蛇不出来... 正解是用类似于 dijkstra 的算法对所有点进行松弛. 对于每个元素记录两个值: \(cost\) 代表 ...

  7. BeanFactory到WebApplicationContext的结构 以及bean和spring容器的关系

    BeanFactory: Ioc 容器 ApplicationContext: Spring容器 WebApplicationContext需要ServletContext实例,也就是说它必须在拥有W ...

  8. Codevs 1010 过河卒== 洛谷 1002

     时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同 ...

  9. vue slot 插槽备忘

    老是记不住插槽咋回事 记录下来备忘 父组件 <tab><template slot="boy" slot-scope="test">{{ ...

  10. es6总结 (五)--函数扩展