AttributeSet 的值用法

http://blog.csdn.net/lmj623565791/article/details/45022631
http://blog.csdn.net/nanzhiwen666/article/details/12224489

http://www.runoob.com/w3cnote/android-tutorial-xfermode-porterduff3.html
https://www.2cto.com/kf/201603/494633.html

一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:



    
        
        
            
            
        
    

二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:

public class CircleImageView extends ImageView {

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //取出attrs中我们为View设置的相关值
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
        mBorderRadius = tArray.getDimensionPixelSize(R.styleable.CircleImageView_Radius, BODER_RADIUS_DEFAULT);
        type = tArray.getInt(R.styleable.CircleImageView_type, TYPE_CIRCLE);
        tArray.recycle();
    }


}

三、接下来在XML布局中引用自定义View控件,其XML代码如下:



    

你可能感兴趣的:(AttributeSet 的值用法)