TypedArray和attrs.xml

TypedArray和attrs.xml和AttributeSet这一系列都是自定义控件属性时要用到的内容

首先说说attrs.xml:它是定义成类似于这种形式的。

  
  
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="MyView">
  4. <attrname="textColor"format="color"></attr>
  5. <attrname="textSize"format="dimension"/>
  6. </declare-styleable>
  7. </resources>

1.主要讲讲里面的format属性

. reference:参考某一资源ID。

(1)属性定义:

  
  
  1. <declare-styleablename="名称">
  2. <attrname="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-styleablename="名称">
  2. <attrname="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-styleablename="名称">
  2. <attrname="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. <declare-styleablename="名称">
  2. <attrname="layout_width"format="dimension"/>
  3. </declare-styleable>

(2)属性使用:

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

⑤. float:浮点值。

(1)属性定义:

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

(2)属性使用:

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

⑥. integer:整型值。

(1)属性定义:

  
  
  1. <declare-styleablename="AnimatedRotateDrawable">
  2. <attrname="visible"/>
  3. <attrname="frameDuration"format="integer"/>
  4. <attrname="framesCount"format="integer"/>
  5. <attrname="pivotX"/>
  6. <attrname="pivotY"/>
  7. <attrname="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"

⑦. string:字符串。

(1)属性定义:

  
  
  1. <declare-styleablename="MapView">
  2. <attrname="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-styleablename="RotateDrawable">
  2. <attrname="visible"/>
  3. <attrname="fromdegrees"format="float"/>
  4. <attrname="todegrees"format="float"/>
  5. <attrname="pivotX"format="fraction"/>
  6. <attrname="pivotY"format="fraction"/>
  7. <attrname="drawable"/>
  8. </declare-styleable>

(2)属性使用:

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

⑨. enum:枚举值

(1)属性定义:

  
  
  1. <declare-styleablename="名称">
  2. <attrname="orientation">
  3. <enumname="horizontal"value="0"/>
  4. <enumname="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>

10. flag:位或运算。

(1)属性定义:

  
  
  1. <declare-styleablename="名称">
  2. <attrname="windowSoftInputMode">
  3. <flagname="stateUnspecified"value="0"/>
  4. <flagname="stateUnchanged"value="1"/>
  5. <flagname="stateHidden"value="2"/>
  6. <flagname="statealwaysHidden"value="3"/>
  7. <flagname="stateVisible"value="4"/>
  8. <flagname="statealwaysVisible"value="5"/>
  9. <flagname="adjustUnspecified"value="0x00"/>
  10. <flagname="adjustResize"value="0x10"/>
  11. <flagname="adjustPan"value="0x20"/>
  12. <flagname="adjustnothing"value="0x30"/>
  13. </attr>
  14. </declare-styleable>

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


注意:

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

(1)属性定义:

  
  
  1. <declare-styleablename="名称">
  2. <attrname="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. />

下面是一个布局文件

  
  
  1. <?xml
  2. version="1.0"encoding="utf-8"?>
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. //自定义的View的路径为com.xc.demo
  6. xmlns:test="http://schemas.android.com/apk/res/com.xc.demo"
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. >
  11. <TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello"
  15. />
  16. <com.xc.demo.MyView
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. //这里引用改变字体和颜色 textColor和textSize都是根据前面的attrs.xml文件设置的
  20. test:textSize="100dp"
  21. test:textColor="#ff0000"
  22. />
  23. </LinearLayout>

2.TypedArray的作用是在代码中设置。

  
  
  1. publicMyView(Contextcontext,AttributeSetattrs){
  2. this(context);
  3. TypedArraya=context
  4. .obtainStyledAttributes(attrs,R.styleable.MyView);
  5. intcolor=a.getColor(R.styleable.MyView_textColor,0XFF0000FF);
  6. floatsize=a.getDimension(R.styleable.MyView_textSize,50);
  7. mPaint.setColor(color);
  8. mPaint.setTextSize(size);
  9. a.recycle();
  10. }

R.styleable.MyView是attrs.xml中<declare-styleable name="MyView">的名字

具体的设置是依靠名字+"_"+名字得到来设置的。

3.如果代码和xml中都设置了,一般以xml中设置为先。

本文出自 “千寻博客,请务必保留此出处http://www.jb51.cc/article/p-gpxgtuvz-ue.html

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念