美文网首页
自定义属性

自定义属性

作者: GeekGray | 来源:发表于2018-10-02 23:03 被阅读2次

阅读原文

自定义属性

自定义属性步骤

1_创建工程:05.自定义属性

2_创建属性类MyAttributeView继承View

3_创建工程的属性文件attrs.xml和常见属性类型

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:atguigu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.atguigu.autoattrs.MyAttributeView
        atguigu:my_name="Android0712"
        atguigu:my_age="100"
        atguigu:my_bg="@drawable/mm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

</RelativeLayout>

查看View的属性

E:\Android_source\2.3源码\JB\frameworks\base\core\res\res\values\attrs.xml

4_使用自定义类并和自定义属性

 <?xml version="1.0"encoding="utf-8"?>
<resources>

    <!--定义一个名字叫MyAttributeView属性集合 -->
    <declare-styleablename="MyAttributeView">
        <!--定义一个类型为string,名字叫my_name的属性  -->
        <attrname="my_name"format="string"/>
         <!--定义一个类型为integer,名字叫my_age的属性  -->
        <attrname="my_age"format="integer"/>
         <!--定义一个类型为reference,名字叫my_bg的属性  -->
        <attrname="my_bg"format="reference"/>
    </declare-styleable>

</resources>

5_三种方式得到属性值

// 三种方式从AttributeSet取对应的属性

//1
.用命名空间取对应的属性- 
     String
//xmlns:atguigu="http://schemas.android.com/apk/res/com.atguigu.autoattrs"
String name = attrs.getAttributeValue(
      "http://schemas.android.com/apk/res-auto",
      "my_name");
String my_age = attrs.getAttributeValue(
      "http://schemas.android.com/apk/res-auto",
      "my_age");
String my_bg = attrs.getAttributeValue(
      "http://schemas.android.com/apk/res-auto",
      "my_bg");
//S
ystem.out.println(name+","+my_age+","+my_bg);

// 2.遍历方式取属性String 和int
for (int i 
     = 0; i < 
     attrs.getAttributeCount(); i++) {
  System.out.println(attrs.getAttributeValue(i)+
 ":"
       + attrs.getAttributeName(i));
}

//3
.用系统工具TypedArray取属性- 取各种值Bitmap 
     String 等等
TypedArrayta = 
     context.obtainStyledAttributes(attrs,
     R.styleable.MyAttributeView);
for (int i = 0; i <t
a.getIndexCount(); i++) {
  int index = ta.getIndex(i);
  switch (index) {
  case R.styleable.MyAttributeView_my_name:// 姓名
         nameStr = 
     ta.getString(index);

     break;
  case R.styleable.MyAttributeView_my_age:// 年龄
         ageInt = 
     ta.getInt(index, 0);

     break;

  case R.styleable.MyAttributeView_my_bg:// 背景
         Drawable drawable =t
a.getDrawable(index);
     if(drawable instanceof BitmapDrawable){
       System.out.println("drawable instanceof 
     BitmapDrawable");
     }
     BitmapDrawablebitmapDrawable =(
BitmapDrawable) drawable;
     bitmap = bitmapDrawable.getBitmap();

     break;

  default:
     break;
  }
}

6_把取到的属性值给给画出来

 @Override
protectedv
oidonDraw(Canvasc
anvas) {
  super.onDraw(canvas);
  Paint paint = new Paint();
  paint.setColor(Color.RED);
  System.out.println("onDrawonDrawonDraw"+getWidth()/2+g
etHeight()/2);
  canvas.drawText(nameStr+"-------------"+ageInt, 50,50, paint );
  canvas.drawBitmap(bitmap,50,50 , paint);
}

相关文章

网友评论

      本文标题:自定义属性

      本文链接:https://www.haomeiwen.com/subject/oqaaoftx.html