[关闭]
@wangwangheng 2014-12-07T18:07:53.000000Z 字数 4025 阅读 2819

自定义View的declare-styleable中format详解

自定义View


本文转载自CSDN博客,并进行了编辑

我们在做项目的时候,由于android自带的属性不能满足需求,android提供了自定义属性的方法,其中的format是做什么用的?以及如何使用它?下面列出一些常用的。

reference:参考某一资源ID。

(1)属性定义:

  1. <declare-styleable name = "名称">
  2. <attr name = "background" format = "reference" />
  3. </declare-styleable>

(2)属性使用:

  1. <ImageView
  2. android:layout_width = "42dip"
  3. android:layout_height = "42dip"
  4. android:background = "@drawable/图片ID"
  5. />

color:颜色值。

(1)属性定义:

  1. <declare-styleable name = "名称">
  2. <attr name = "textColor" format = "color" />
  3. </declare-styleable>

(2)属性使用:

  1. <TextView
  2. android:layout_width = "42dip"
  3. android:layout_height = "42dip"
  4. android:textColor = "#00FF00"
  5. />

boolean:布尔值。

(1)属性定义:

  1. <declare-styleable name = "名称">
  2. <attr name = "focusable" format = "boolean" />
  3. </declare-styleable>

(2)属性使用:

  1. <Button
  2. android:layout_width = "42dip"
  3. android:layout_height = "42dip"
  4. android:focusable = "true"
  5. />

dimension:尺寸值。

  1. 1)属性定义:
  2. <declare-styleable name = "名称">
  3. <attr name = "layout_width" format = "dimension" />
  4. </declare-styleable>

(2)属性使用:

  1. <Button
  2. android:layout_width = "42dip"
  3. android:layout_height = "42dip"
  4. />

float:浮点值。

(1)属性定义:

  1. <declare-styleable name = "AlphaAnimation">
  2. <attr name = "fromAlpha" format = "float" />
  3. <attr name = "toAlpha" format = "float" />
  4. </declare-styleable>

(2)属性使用:

  1. <alpha
  2. android:fromAlpha = "1.0"
  3. android:toAlpha = "0.7"
  4. />

integer:整型值。

(1)属性定义:

  1. <declare-styleable name = "AnimatedRotateDrawable">
  2. <attr name = "visible" />
  3. <attr name = "frameDuration" format="integer" />
  4. <attr name = "framesCount" format="integer" />
  5. <attr name = "pivotX" />
  6. <attr name = "pivotY" />
  7. <attr name = "drawable" />
  8. </declare-styleable>

(2)属性使用:

  1. <animated-rotate
  2. xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:drawable = "@drawable/图片ID"
  4. android:pivotX = "50%"
  5. android:pivotY = "50%"
  6. android:framesCount = "12"
  7. android:frameDuration = "100"
  8. />

string:字符串。

(1)属性定义:

  1. <declare-styleable name = "MapView">
  2. <attr name = "apiKey" format = "string" />
  3. </declare-styleable>

(2)属性使用:

  1. <com.google.android.maps.MapView
  2. android:layout_width = "fill_parent"
  3. android:layout_height = "fill_parent"
  4. android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
  5. />

fraction:百分数。

(1)属性定义:
  1. <declare-styleable name="RotateDrawable">
  2. <attr name = "visible" />
  3. <attr name = "fromDegrees" format = "float" />
  4. <attr name = "toDegrees" format = "float" />
  5. <attr name = "pivotX" format = "fraction" />
  6. <attr name = "pivotY" format = "fraction" />
  7. <attr name = "drawable" />
  8. </declare-styleable>

(2)属性使用:

  1. <rotate xmlns:android = "http://schemas.android.com/apk/res/android"
  2. android:interpolator = "@anim/动画ID"
  3. android:fromDegrees = "0"
  4. android:toDegrees = "360"
  5. android:pivotX = "200%"
  6. android:pivotY = "300%"
  7. android:duration = "5000"
  8. android:repeatMode = "restart"
  9. android:repeatCount = "infinite"
  10. />

enum:枚举值。

(1)属性定义:

  1. <declare-styleable name="名称">
  2. <attr name="orientation">
  3. <enum name="horizontal" value="0" />
  4. <enum name="vertical" value="1" />
  5. </attr>
  6. </declare-styleable>

(2)属性使用:

  1. <LinearLayout
  2. xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical"
  4. android:layout_width = "fill_parent"
  5. android:layout_height = "fill_parent"
  6. >
  7. </LinearLayout>

flag:位或运算。

(1)属性定义:

  1. <declare-styleable name="名称">
  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>
  14. </declare-styleable>

(2)属性使用:

  1. <activity
  2. android:name = ".StyleAndThemeActivity"
  3. android:label = "@string/app_name"
  4. android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
  5. <intent-filter>
  6. <action android:name = "android.intent.action.MAIN" />
  7. <category android:name = "android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>

属性定义时可以指定多种类型值

(1)属性定义:

  1. <declare-styleable name = "名称">
  2. <attr name = "background" format = "reference|color" />
  3. </declare-styleable>

(2)属性使用:

  1. <ImageView
  2. android:layout_width = "42dip"
  3. android:layout_height = "42dip"
  4. android:background = "@drawable/图片ID|#00FF00"
  5. />
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注