美文网首页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 的值用法

    http://blog.csdn.net/lmj623565791/article/details/4502263...

  • AttributeSet

    (计划2016/10/18上午完成) 废话不多说,先翻译一波官方文档: AttributeSet是一组属性的集合,...

  • 自定义view的自定义属性备忘处理

    public CountDownProgress(Context context, AttributeSet at...

  • CollapsingToolbarLayout属性

    //获取新的布局参数 generateLayoutParams(AttributeSet attrs) Retur...

  • ES6之解构赋值详解

    数组的解构赋值 基础用法 指定默认值 对象的解构赋值 基础用法 指定默认值默认值生效的条件是,对象的属性值严格等于...

  • ios贝塞尔曲线表格视图

    x轴和y轴都是可自定义,曲线的值也是对应的值 BezierView的用法 用法一:值模式 /**设置x、y@par...

  • R,笔记01

    设置随机数的种子值 grep用法 载入数据 创建数据框 subset用法 strsplit 和 paste用法

  • 04-枚举

    枚举的基本用法 关联值(Associated Values) 关联值举例 原始值(Raw Values) 注:原始...

  • JavaScript isNaN() 函数

    定义和用法isNaN() 函数用于检查其参数是否是非数字值。 isNaN的用法:检测当前值是否不是有效数字,返回t...

  • 函数参数默认值

    基础用法 使用默认值语法设置函数参数的默认值。

网友评论

    本文标题:AttributeSet 的值用法

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