现在的位置: 首页 > 综合 > 正文

自定义控件 attrs.xml

2019年05月06日 ⁄ 综合 ⁄ 共 8070字 ⁄ 字号 评论关闭

attrs.xml format属性说明:

[html] view
plain
copy

  1. <attr name="xxx"<strong> format</strong>="..."/>   

[html] view
plain
copy

  1. "reference" //引用  
  2. "color" //颜色  
  3. "boolean" //布尔值  
  4. "dimension" //尺寸值  
  5. "float" //浮点值  
  6. "integer" //整型值  
  7. "string" //字符串  
  8. "fraction" //百分数,比如200%  
[html] view
plain
copy

  1. 枚举型的格式:  
  2. <attr name="orientation">  
  3.   <enum name="horizontal" value="0" />  
  4.   <enum name="vertical" value="1" />  
  5. </attr>  
[html] view
plain
copy

  1. XML文件中使用:  
  2. android:orientation = "vertical"   
[html] view
plain
copy

  1.   
[html] view
plain
copy

  1. 标志位、位或运算,格式如下:  
  2. <attr name="windowSoftInputMode">  
  3.   <flag name = "stateUnspecified" value = "0" />  
  4.   <flag name = "stateUnchanged" value = "1" />  
  5.   <flag name = "stateHidden" value = "2" />  
  6.   <flag name = "stateAlwaysHidden" value = "3" />  
  7.   <flag name = "stateVisible" value = "4" />  
  8.   <flag name = "stateAlwaysVisible" value = "5" />  
  9.   <flag name = "adjustUnspecified" value = "0x00" />  
  10.   <flag name = "adjustResize" value = "0x10" />  
  11.   <flag name = "adjustPan" value = "0x20" />  
  12.   <flag name = "adjustNothing" value = "0x30" />  
  13. </attr>  
[html] view
plain
copy

  1. XML文件中使用:  
  2. android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">    
[html] view
plain
copy

  1.   
[html] view
plain
copy

  1. 属性定义可以指定多种类型:  
  2. <attr name = "background" format = "reference|color" />    
[html] view
plain
copy

  1. XML文件中使用:  
  2. android:background = "@drawable/图片ID|#00FF00"    

 attrs.xml  format使用说明:

[html] view
plain
copy

  1. 1. reference:参考某一资源ID。  
  2.     (1)属性定义:  
  3.            <declare-styleable name = "名称">  
  4.                   <attr name = "background" format = "reference" />  
  5.            </declare-styleable>  
  6.     (2)属性使用:  
  7.             <ImageView  
  8.                     android:layout_width = "42dip"  
  9.                     android:layout_height = "42dip"  
  10.                     android:background = "@drawable/图片ID"  
  11.                     />  
  12. 2. color:颜色值。  
  13.     (1)属性定义:  
  14.            <declare-styleable name = "名称">  
  15.                   <attr name = "textColor" format = "color" />  
  16.            </declare-styleable>  
  17.     (2)属性使用:  
  18.             <TextView  
  19.                     android:layout_width = "42dip"  
  20.                     android:layout_height = "42dip"  
  21.                     android:textColor = "#00FF00"  
  22.                     />  
  23. 3. boolean:布尔值。  
  24.     (1)属性定义:  
  25.            <declare-styleable name = "名称">  
  26.                   <attr name = "focusable" format = "boolean" />  
  27.            </declare-styleable>  
  28.     (2)属性使用:  
  29.             <Button  
  30.                    android:layout_width = "42dip"  
  31.                    android:layout_height = "42dip"  
  32.                    android:focusable = "true"  
  33.                    />  
  34. 4. dimension:尺寸值。  
  35.     (1)属性定义:  
  36.            <declare-styleable name = "名称">  
  37.                   <attr name = "layout_width" format = "dimension" />  
  38.            </declare-styleable>  
  39.     (2)属性使用:  
  40.             <Button  
  41.                    android:layout_width = "42dip"  
  42.                    android:layout_height = "42dip"  
  43.                    />  
  44. 5. float:浮点值。  
  45.     (1)属性定义:  
  46.            <declare-styleable name = "AlphaAnimation">  
  47.                   <attr name = "fromAlpha" format = "float" />  
  48.                   <attr name = "toAlpha" format = "float" />  
  49.            </declare-styleable>  
  50.     (2)属性使用:  
  51.             <alpha  
  52.                   android:fromAlpha = "1.0"  
  53.                   android:toAlpha = "0.7"  
  54.                   />  
  55. 6. integer:整型值。  
  56.     (1)属性定义:  
  57.            <declare-styleable name = "AnimatedRotateDrawable">  
  58.                   <attr name = "visible" />  
  59.                   <attr name = "frameDuration" format="integer" />  
  60.                   <attr name = "framesCount" format="integer" />  
  61.                   <attr name = "pivotX" />  
  62.                   <attr name = "pivotY" />  
  63.                   <attr name = "drawable" />  
  64.            </declare-styleable>  
  65.     (2)属性使用:  
  66.            <animated-rotate  
  67.                   xmlns:android = "http://schemas.android.com/apk/res/android"   
  68.                   android:drawable = "@drawable/图片ID"   
  69.                   android:pivotX = "50%"   
  70.                   android:pivotY = "50%"   
  71.                   android:framesCount = "12"   
  72.                   android:frameDuration = "100"  
  73.                   />  
  74. 7. string:字符串。  
  75.     (1)属性定义:  
  76.            <declare-styleable name = "MapView">  
  77.                   <attr name = "apiKey" format = "string" />  
  78.            </declare-styleable>  
  79.     (2)属性使用:  
  80.            <com.google.android.maps.MapView  
  81.                    android:layout_width = "fill_parent"  
  82.                    android:layout_height = "fill_parent"  
  83.                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"  
  84.                    />  
  85. 8. fraction:百分数。  
  86.     (1)属性定义:  
  87.            <declare-styleable name="RotateDrawable">  
  88.                   <attr name = "visible" />  
  89.                   <attr name = "fromDegrees" format = "float" />  
  90.                   <attr name = "toDegrees" format = "float" />  
  91.                   <attr name = "pivotX" format = "fraction" />  
  92.                   <attr name = "pivotY" format = "fraction" />  
  93.                   <attr name = "drawable" />  
  94.            </declare-styleable>  
  95.     (2)属性使用:  
  96.            <rotate  xmlns:android = "http://schemas.android.com/apk/res/android"  
  97.               android:interpolator = "@anim/动画ID"  
  98.                 android:fromDegrees = "0"  
  99.               android:toDegrees = "360"  
  100.                 android:pivotX = "200%"  
  101.                 android:pivotY = "300%"  
  102.               android:duration = "5000"  
  103.                 android:repeatMode = "restart"  
  104.                 android:repeatCount = "infinite"  
  105.                   />  
  106. 9. enum:枚举值。  
  107.     (1)属性定义:  
  108.            <declare-styleable name="名称">  
  109.                   <attr name="orientation">  
  110.                          <enum name="horizontal" value="0" />  
  111.                          <enum name="vertical" value="1" />  
  112.                   </attr>             
  113.            </declare-styleable>  
  114.     (2)属性使用:  
  115.            <LinearLayout  
  116.                    xmlns:android = "http://schemas.android.com/apk/res/android"  
  117.                    android:orientation = "vertical"  
  118.                    android:layout_width = "fill_parent"  
  119.                    android:layout_height = "fill_parent"  
  120.                    >  
  121.            </LinearLayout>  
  122. 10. flag:位或运算。  
  123.      (1)属性定义:  
  124.             <declare-styleable name="名称">  
  125.                    <attr name="windowSoftInputMode">  
  126.                            <flag name = "stateUnspecified" value = "0" />  
  127.                            <flag name = "stateUnchanged" value = "1" />  
  128.                            <flag name = "stateHidden" value = "2" />  
  129.                            <flag name = "stateAlwaysHidden" value = "3" />  
  130.                            <flag name = "stateVisible" value = "4" />  
  131.                            <flag name = "stateAlwaysVisible" value = "5" />  
  132.                            <flag name = "adjustUnspecified" value = "0x00" />  
  133.                            <flag name = "adjustResize" value = "0x10" />  
  134.                            <flag name = "adjustPan" value = "0x20" />  
  135.                            <flag name = "adjustNothing" value = "0x30" />  
  136.                     </attr>          
  137.             </declare-styleable>  
  138.      (2)属性使用:  
  139.             <activity  
  140.                   android:name = ".StyleAndThemeActivity"  
  141.                   android:label = "@string/app_name"  
  142.                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">  
  143.                   <intent-filter>  
  144.                          <action android:name = "android.intent.action.MAIN" />  
  145.                          <category android:name = "android.intent.category.LAUNCHER" />  
  146.                   </intent-filter>  
  147.             </activity>  
  148.      注意:  
  149.      属性定义时可以指定多种类型值。  
  150.     (1)属性定义:  
  151.            <declare-styleable name = "名称">  
  152.                   <attr name = "background" format = "reference|color"/>  
  153.            </declare-styleable>  
  154.     (2)属性使用:  
  155.             <ImageView  
  156.                     android:layout_width = "42dip"  
  157.                     android:layout_height = "42dip"  
  158.                     android:background = "@drawable/图片ID|#00FF00"  
  159.                     />  

抱歉!评论已关闭.