自定义控件3.自定义属性

来源:互联网 发布:win7触控板手势软件 编辑:程序博客网 时间:2024/05/21 14:08
在上一篇里, 自定义View的背景色和文字颜色都是在 onDraw 里写死的, 这次我们实现可以通过布局文件配置. 要实现这个功能, 需要有如下几个步骤:

1. 创建 attrs.xml

在 res/values 目录下新建一个 attrs.xml , 内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!-- declare CustomView2 style -->
  4. <declare-styleable name="CustomView2">
  5. <attr name="textColor" format="color" />
  6. <attr name="backgroundColor" format="color" />
  7. </declare-styleable>
  8. </resources>

2. 在布局文件中使用

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:app="http://schemas.android.com/apk/res/com.gaoyuan4122.customui"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <TextView
  7. android:id="@+id/tv_hello"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello_world" />
  11. <com.gaoyuan4122.customui.views.CustomView1
  12. android:id="@+id/cv_custom1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_below="@id/tv_hello" />
  16. <TextView
  17. android:id="@+id/tv_hello2"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="@string/hello_world"
  21. android:layout_below="@id/cv_custom1" />
  22. <com.gaoyuan4122.customui.views.CustomView2
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_below="@id/tv_hello2"
  26. app:backgroundColor="#ff00ff"
  27. app:textColor="#ffff00" />
  28. </RelativeLayout>
注意:
1. 要在最外层的控件中加一个新的名称空间, 比如上面叫 xmlns:app="...", 这里的名字可以随便取.
2. 在控件上使用的时候, 要加上命名空间, 注意和上面定义的保持一致.

3. 在自定义 View 中使用

自定义属性需要在构造方法中获取, 上一篇文章里说了, 继承 View 需要实现两个构造方法, 一个是只有 Context 参数的, 一个是有 Context, AttributeSet 参数的, 其实自定义 View 还有一个构造方法, 有三个参数: Context, AttributeSet, int(defStyleAttr), 第二个参数 AttributeSet 封装了View的属性信息, 我们自定义的属性会被封装到这个类里, 第三个参数和样式有关, 这里先不说. View的三个构造方法一般是下面的写法:
  1. private int mTextColor;
  2. private int mBackgroundColor;
  3. public CustomView2(Context context) {
  4. this(context, null);
  5. }
  6. public CustomView2(Context context, AttributeSet attrs) {
  7. this(context, attrs, 0);
  8. }
  9. // 自定义属性会被封装到 attrs 中, 第三个参数和样式有关, 这里先不说.
  10. public CustomView2(Context context, AttributeSet attrs, int defStyleAttr) {
  11. super(context, attrs, defStyleAttr);
  12. // 通过下面的方法去除自定义属性
  13. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView2);
  14. mTextColor = typedArray.getColor(R.styleable.CustomView2_textColor, Color.RED);
  15. mBackgroundColor = typedArray.getColor(R.styleable.CustomView2_backgroundColor, Color.BLUE);
  16. // 最后一定要注意调用这个方法, 至于为什么后面会说.
  17. typedArray.recycle();
  18. }
通过上面的方式可以将自定义属性取出来, 然后就可以使用了:
  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. mPaint.setStyle(Style.FILL);
  5. mPaint.setColor(mBackgroundColor);
  6. mRect.set(0, 0, 100, 100);
  7. canvas.drawRect(mRect, mPaint);
  8. mPaint.setColor(mTextColor);
  9. mPaint.setTextSize(30.0f);
  10. canvas.drawText("Hello!", 0, 100, mPaint);
  11. }
注意别忘了重写 onMeasure 方法, 否则又会出现 View 的宽高为 match_parent.
最后的效果如下:
 

4. 关于自定义属性的 format

自定义属性的时候, <attrs> 标签中有一个 format 属性, 这个属性值表示将用户传入的值以何种方式进行解析, 比如解析成一个字符串, 或者一个 color 值等等, 下面列举了所有可以解析的类型:
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"

                     />


来自为知笔记(Wiz)


0 0
原创粉丝点击