TypedArray和attrs.xml

前端之家收集整理的这篇文章主要介绍了TypedArray和attrs.xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

. reference:参考某一资源ID。

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="background"format="reference"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

②. color:颜色值。

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="textColor"format="color"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

③. boolean:布尔值。

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="focusable"format="boolean"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

④. dimension:尺寸值。

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="layout_width"format="dimension"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

⑤. float:浮点值。

(1)属性定义:

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

(2)属性使用:

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

⑥. integer:整型值。

(1)属性定义:

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

(2)属性使用:

  
  
  1. @H_502_9@<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. @H_502_9@<declare-styleablename="MapView"@H_502_9@>
  2. @H_502_9@<attrname="apiKey"format="string"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

⑧. fraction:百分数。

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="RotateDrawable"@H_502_9@>
  2. @H_502_9@<attrname="visible"@H_502_9@/>
  3. @H_502_9@<attrname="fromDegrees"format="float"@H_502_9@/>
  4. @H_502_9@<attrname="toDegrees"format="float"@H_502_9@/>
  5. @H_502_9@<attrname="pivotX"format="fraction"@H_502_9@/>
  6. @H_502_9@<attrname="pivotY"format="fraction"@H_502_9@/>
  7. @H_502_9@<attrname="drawable"@H_502_9@/>
  8. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

  
  
  1. @H_502_9@<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. @H_502_9@/>

⑨. enum:枚举值

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="orientation"@H_502_9@>
  3. @H_502_9@<enumname="horizontal"value="0"@H_502_9@/>
  4. @H_502_9@<enumname="vertical"value="1"@H_502_9@/>
  5. @H_502_9@</attr@H_502_9@>
  6. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

  
  
  1. @H_502_9@<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. @H_502_9@>
  7. @H_502_9@</LinearLayout@H_502_9@>

10. flag:位或运算。

(1)属性定义:

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

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


注意:

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

(1)属性定义:

  
  
  1. @H_502_9@<declare-styleablename="名称"@H_502_9@>
  2. @H_502_9@<attrname="background"format="reference|color"@H_502_9@/>
  3. @H_502_9@</declare-styleable@H_502_9@>

(2)属性使用:

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

下面是一个布局文件

  
  
  1. @H_502_9@<?xml
  2. version="1.0"encoding="utf-8"@H_502_9@?>
  3. @H_502_9@<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. @H_502_9@>
  11. @H_502_9@<TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello"
  15. @H_502_9@/>
  16. @H_502_9@<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. @H_502_9@/>
  23. @H_502_9@</LinearLayout@H_502_9@>

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

猜你在找的XML相关文章