码迷,mamicode.com
首页 > 其他好文 > 详细

自定义属性,使用步骤

时间:2017-12-26 17:55:28      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:pixel   enc   inf   efs   info   ext.get   新建   reference   fill   

1.values 文件夹下,新建attrs.xml,在resource节点下写:

 <declare-styleable name="FillInInfoView">
        <attr name="pic" format="reference"/>
        <attr name="etHint" format="string"/>
    </declare-styleable>

2.在自定义view里面获取自定义属性

第一种方式:

   TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.FillInInfoView);
        Drawable drawable = ta.getDrawable(R.styleable.FillInInfoView_pic);
        String string = ta.getString(R.styleable.FillInInfoView_etHint);
        ta.recycle();

第二种方式:

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int arr = a.getIndex(i);
            switch (arr) {
                case R.styleable.CircleView_innerCircleColor:
                    innerColor = a.getColor(arr, Color.BLUE);
                    break;
                case R.styleable.CircleView_innerCircleSize:
                    innerCircleSize = a.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CircleView_outCircleSize:
                    outCircleSize = a.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CircleView_smallCircleSize:
                    outSmallCircleSize = a.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CircleView_smallCircleRadiousSize:
                    outSmallCircleRadiousSize = a.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();

 

自定义属性,使用步骤

标签:pixel   enc   inf   efs   info   ext.get   新建   reference   fill   

原文地址:https://www.cnblogs.com/hechangshou/p/8118358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!