美文网首页Android
AttributeSet 的值用法

AttributeSet 的值用法

作者: 涛涛123759 | 来源:发表于2017-09-06 14:01 被阅读274次

    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文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="CircleImageView">
            <attr name="Radius" format="dimension"/>
            <attr name="type">
                <enum name="circle" value="0"/>
                <enum name="round" value="1"/>
            </attr>
        </declare-styleable>
    </resources>
    

    二、接下来实现自定义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代码如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.jay.xfermodedemo1.CircleImageView
            android:layout_width="160dp"
            android:layout_height="280dp"
            android:layout_margin="10dp"
            android:src="@mipmap/ic_bg_meizi1"
            app:Radius="30dp"
            app:type="round" />
    
    </LinearLayout>

    相关文章

      网友评论

        本文标题:AttributeSet 的值用法

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