`
h416756139
  • 浏览: 358573 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android中attr自定义属性详解

 
阅读更多
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wen=http://schemas.android.com/apk/res/com.iteye.googlers
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

第二行是自定义标签。

格式如上,其中“xmlns:wen”冒号后面是标签名,在下面使用时(只对当前文件可用)

<TextView  wen:属性名/>

“com.iteye.googlers”是你的工程包名。


1. reference:参考某一资源ID。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "background" format = "reference" />
            </declare-styleable>
    (2)属性使用:
            <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID"
                     />

2. color:颜色值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "textColor" format = "color" />
            </declare-styleable>
    (2)属性使用:
            <TextView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"
                     />

 3. boolean:布尔值。
    (1)属性定义:
            <declare-styleable name = "名称">
                <attr name = "focusable" format = "boolean" />
            </declare-styleable>
    (2)属性使用:
            <Button
                   android:layout_width = "42dip"
                   android:layout_height = "42dip"
                   android:focusable = "true"
                    />
 
4. dimension:尺寸值。
     (1)属性定义:
             <declare-styleable name = "名称">
                   <attr name = "layout_width" format = "dimension" />
            </declare-styleable>
    (2)属性使用:
            <Button
                   android:layout_width = "42dip"
                   android:layout_height = "42dip"
                  />

 5. float:浮点值。
    (1)属性定义:
            <declare-styleable name = "AlphaAnimation">
                   <attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />
            </declare-styleable>
    (2)属性使用:
            <alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"
                   />
 
6. integer:整型值。
    (1)属性定义:
            <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>
    (2)属性使用:
            <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:字符串。
    (1)属性定义:
            <declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>
    (2)属性使用:
            <com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
                    />

8. fraction:百分数。
     (1)属性定义:
            <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>

    (2)属性使用:
            <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:枚举值。
    (1)属性定义:
            <declare-styleable name="名称">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            
            </declare-styleable>
    (2)属性使用:
            <LinearLayout
                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
            </LinearLayout>

10. flag:位或运算。
     (1)属性定义:
             <declare-styleable name="名称">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>         
             </declare-styleable>

     (2)属性使用:
            <activity
                   android:name = ".StyleAndThemeActivity"
                   android:label = "@string/app_name"
                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
                   <intent-filter>
                          <action android:name = "android.intent.action.MAIN" />
                          <category android:name = "android.intent.category.LAUNCHER" />
                   </intent-filter>
             </activity>
     注意:
     属性定义时可以指定多种类型值。
    (1)属性定义:
            <declare-styleable name = "名称">
                   <attr name = "background" format = "reference|color" />
            </declare-styleable>
    (2)属性使用:
             <ImageView
                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/图片ID|#00FF00"
                     />
分享到:
评论

相关推荐

    android开发教程之自定义属性用法详解

    自定义属性都存在于/value/attr.xml文件中,以如下格式存在。 代码如下: ”自定义属性名称”&gt; &lt;attr name=”属性名称” format=”属性种类”/&gt; …… 对于自定义属性中的format的值及其含义如下: format属性值:...

    Android自定义控件属性详细介绍

    Android自定义控件属性详细介绍 1. reference:参考某一资源ID。   (1)属性定义: &lt;attr xss=removed xss=removed&gt;  (2)属性使用: &lt;ImageView android:layout_width = 42dip android:layout_...

    Android中自定义进度条详解

    Android原生控件只有横向进度条一种,而且没法变换样式,比如原生rom的样子 很丑是吧,当伟大的产品设计要求更换前背景,...android:attr/progressBarStyleHorizontal”  android:layout_width=”match_parent”  

    详解Android自定义控件属性

    在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢? 在文件res/values/下新建attrs.xml属性文件,中定义我们所需要的属性。 ...

    详解Android自定义控件属性TypedArray以及attrs

    大家也可以结合《理解Android中的自定义属性》这篇文章进行学习,后续一篇还有应用。 1、attrs文件编写 &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;resources&gt; &lt;attr name=titleText format=string&gt; &lt;attr ...

    Android自定义View中attrs.xml的实例详解

    Android自定义View中attrs.xml的实例详解 我们在自定义View的时候通常需要先完成attrs.xml文件 在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性。 &lt;?xml version=1.0 ...

    Android自定义滑动解锁控件使用详解

    可以自定义的属性有: &lt;!-- 滑动解锁控件 xml配置属性 --&gt; &lt;attr name=slideImageViewWidth format=dimension/&gt;&lt;!-- 滑块宽度 --&gt; &lt;attr name=slideImageViewResId format=reference/&gt;&lt;!-- 滑块...

    Android自定义控件深入学习 Android生成随机验证码

    在上一篇的文章中介绍了自定义控件的属性,详情见《详解Android自定义控件属性TypedArray以及attrs》。那么在这基础上实现随机验证码生成,里面的代码是自定义控件以及涉及到自定义view绘画。 1、先看实现的效果图 ...

    Android ViewPagerIndicator详解及实例代码

    自定义View和自定义属性的知识不再此提及,这里着重说的是属性在自定义View中的获取方式,自定义的属性如下: &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;resources&gt; &lt;attr name=wisely_1 format=boolean&gt; ...

    Android自定义View实现纵向跑马灯效果详解

    首先看看效果图(录制的gif有点卡,真实的效果还是很流畅的) ...首先定义一些要用到的属性 &lt;attr name=textSize format=dimension&gt; &lt;attr name=textColor format=color&gt; &lt;attr name=paddingLeft format=dimension

    Android-StickerView:支持缩放和拖动的贴纸视图。 通常用于将位图标记到另一个。 例如:pdf

    Android-StickerView 支持缩放和拖动的标签视图。 通常用于将位图标记到另一个。...自定义属性详解 &lt; ! -- sticker初始化宽高 -- &gt; &lt; attr xss=removed xss=removed&gt; &lt; attr name = " stv_s

    Android自定义View实现验证码

    本文章是基于鸿洋的Android 自定义View (一) 的一些扩展,以及对Android自定义View构造函数详解里面内容的一些转载。 首先我们定义一个declare-styleable标签declare-styleable标签的作用是给自定义控件添加自定义...

    Android 开发订单流程view实例详解

    Android 开发订单流程view实例详解 先看看最终效果图: 怎么样,效果还是很不错的吧?群里有人说切四张图的、recycleview的、各种的都有啊,但是最简单的就是通过自定义view来实现了~接下来让我们来实现下这个...

    Android BannerView通用封装详解

    1、自定义属性 selectPoint:选中指示器图标 normalPoint:未选中指示器图标 pointWidth:图标宽度 switchTime:轮播间隔事件 location:指示器位置,下中或下右 &lt;attr name=selectPoint format=reference&gt; &lt...

    android实现直播点赞飘心动画效果

    在attrs.xml 中增加自定义的属性 &lt;!-- 飘心动画自定义的属性 --&gt; &lt;attr name=initX format=dimension/&gt; &lt;attr name=initY format=dimension/&gt; &lt;attr name=xRand format=dimension/&gt; &lt;attr name=...

Global site tag (gtag.js) - Google Analytics