View (二) 自定义属性 自定义属性的格式详解

简介:

自定义属性格式一共有十种;

1. reference:参考某一资源ID。

2. color:颜色值。

3. boolean:布尔值。

4. dimension:尺寸值。

5. float:浮点值。

6. integer:整型值。

7. string:字符串。

8. fraction:百分数。

9. enum:枚举值。

10. flag:位或运算。

 

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"
/>
复制代码

 


    本文转自 一点点征服   博客园博客,原文链接:http://www.cnblogs.com/ldq2016/p/5200758.html,如需转载请自行联系原作者





相关文章
|
5月前
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
Thymeleaf内置对象、定义变量、URL参数及标签自定义属性
104 0
|
13天前
|
存储 编译器 C#
自定义标签属性
自定义标签属性
44 0
关于Navisworks属性查看的问题(属性不显示)
关于Navisworks属性查看的问题(属性不显示)
关于Navisworks属性查看的问题(属性不显示)
|
JavaScript
【笔记】js对象通过属性路径获取属性值
js对象通过属性路径获取属性值
300 0
SwiftUI—Text视图的填充属性
SwiftUI—Text视图的填充属性
285 0
SwiftUI—Text视图的填充属性
|
前端开发 索引 JavaScript
classList属性详解
之前我们要操作一个DOM元素的class属性,需要对这个DOM的class进行繁琐的循环判断,而现在HTML5为每个元素定义了classLlist属性,用于在元素中添加,移除及切换 CSS 类。该属性是 DOMTokenList 对象(一个只读的类数组对象),你可以通过DOMTokenList定义的方法对其进行修改。
1374 0
|
XML 前端开发 Android开发