自定义属性的实现

Attribute 属性

Custom 自定义

 

常用的自定义属性

Format 常用类型

Reference 引用

Color 颜色

Boolean 布尔值

Dimension 尺寸值

Float 浮点值

Integer 整形值

String 字符串

Enum 枚举


自定义属性的参照,可以到sdk的platforms中任意一个版本下的data下的res的value中的attrs打开查看view。

                 

这是系统规定的枚举类型的属性,只有三个值:可见,不可见但占位,不可见


reference代表是引用类型 这是规定控件的id

 

由上可知name规定属性名,format规定属性类型。

 

自定义属性中的属性的类型是reference是引用类型,代表可以是引用值。例如引用string.xml文件中的内容,或引用图片。

 

自定义属性的实现

新建一个xml引用资源,在其中新建declare-styleable节点 name是我们的属性名。


这是我自己规定的我的属性

将我们自定义好的属性使用到我们的布局中,首先需要在布局中的最外层添加zxmlns:myattribute=http://schemas.android.com/apk/res/com.example.myattribute(这是eclipes使用时添加的,对于android studio

xmlns:myattribute="http://schemas.android.com/apk/res-auto")

com.example.myattribute是所在项目的包名,myattribute前缀是任意取的,使用时是以这个开头的。

 然后我们就可以引用我们自己的属性了

myattribute是我们自己规定的属性集合名,my_bgmy_name是我们自己规定属性名称。


自定义控件中获取属性的方法

有三种获取属性的方法

在自定义控件的构造方法中

                   //获取属性的三种方式

                   //1.用命名空间获取

// attrs.getAttributeValue(namespace,name) // 我们自定义属性的namespace命名空间,name 属性名

                   Stringmy_name = attrs.getAttributeValue(

                   "http://schemas.android.com/apk/res/com.example.myattribute.attrs","my_name");

// 2.遍历属性集合// 在构造方法中     遍历出的是使用该view时设置的所有属性

                   for(int i = 0; i < attrs.getAttributeCount(); i++) {

                            System.out.println(attrs.getAttributeName(i)

                                               +attrs.getAttributeValue(i) + "\n");

                   }

// 3.使用系统工具获取属性可以获取所有引用该控件时设置的所有的属性

                   //第一个参数是AttributeSet属性参数,第二个是我们要获取的属性集合

                   //TypedArray array=context.obtainStyledAttributes(set, attrs)

                   TypedArrayarray = context.obtainStyledAttributes(attrs,

                                     R.styleable.MyAttributeView);

                   //同样需要遍历出已经设置的所有的属性

                   for(int i = 0; i < array.getIndexCount(); i++) {

                            intindex = array.getIndex(i);

                            switch(index) {

                            caseR.styleable.MyAttributeView_my_bg:

                                     Drawabledrawable=array.getDrawable(index);

                                     BitmapDrawabledrawables=(BitmapDrawable) drawable;

                                     bitmap=drawables.getBitmap();

                                     break;

                            caseR.styleable.MyAttributeView_my_size:

                                     size= array.getInt(index, 0);

                                     break;

                            caseR.styleable.MyAttributeView_my_name:

                                     name= array.getString(index);

                                     break;

                            }

                   }

主要使用的是方法三,这个可以通过查看View的构造方法知道,他的构造方法进入通过点击super



使用:

新建attrs xml资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyAttributte">
        <attr name="custom_name" format="string"/>
        <attr name="custom_color" format="reference|color"/>
        <attr name="custom_bg" format="reference"/>
        <attr name="custom_content" format="reference|string"/>
    </declare-styleable>
</resources>

 

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:custom="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="com.example.administrator.customview2.MainActivity">

    <com.example.administrator.customview2.MyAttributte
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
custom:custom_bg="@drawable/three"
       
custom:custom_content="@string/custom_string"
       
custom:custom_color="@color/colorPrimaryDark"/>

</android.support.constraint.ConstraintLayout>

 

xmlns:custom=http://schemas.android.com/apk/res-auto是引用自定义属性的命名空间 其中custom是我们自定义属性的引用名,针对于eclipes res-auto需要改为包名

Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

 

自定义控件

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/5/21.
 */

public class MyAttributte extends View {

    private String customName;
    private String bgID;//背景的id
    private String colorId;//颜色id
    private Bitmap bgDrawable;
    private int color;
    private Paint paint;
    public MyAttributte(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
        //获取自定义属性的第一种方法
        customName=attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","custom_content");//第一个参数是资源的命名空间,第二个参数是属性名称
        bgID=attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","custom_bg");
        colorId=attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","custom_color");
        System.out.println("custom"+customName+"bgID"+bgID+"colorId"+colorId);
        //获取属性的第二种方式
        for (int i=0;i<attrs.getAttributeCount();i++)
        {
            String name=attrs.getAttributeName(i);//获取属性的名称
            String value=attrs.getAttributeValue(i);//获取属性的值
            System.out.println(name+value);
        }
        //上面两种方式只能获取属性的值和名称,对于图片类型,引用类型没有获取具体的图片和资源结果。
        //获取属性的第三种方式
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyAttributte);//第一个参数是属性,第二个参数是属性集合
        for (int j=0;j<typedArray.getIndexCount();j++)
        {
            int index=typedArray.getIndex(j);
            switch(index)
            {
                case R.styleable.MyAttributte_custom_bg:
                    bgDrawable=((BitmapDrawable) typedArray.getDrawable(index)).getBitmap();
                    break;
                case R.styleable.MyAttributte_custom_content:
                    customName=typedArray.getString(index);
                    break;
                case R.styleable.MyAttributte_custom_color:
                    color=typedArray.getColor(index,0);
                    break;
            }
        }
        paint=new Paint();
        paint.setColor(color);
        paint.setTextSize(18);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(customName,30,30,paint);//画文字
        canvas.drawBitmap(bgDrawable,40,40,paint);//画图
    }
}

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值