笔记,Android获取Style属性值

方式一,通过AttributeSet 获取自定义的属性

 public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }
    private void init(AttributeSet attrs, int defStyle) {
        //首先判断attrs是否为null
        if(attrs != null){
            //获取AttributeSet中所有的XML属性的数量
            int count = attrs.getAttributeCount();
            //遍历AttributeSet中的XML属性
            for(int i = 0; i < count; i++){
                //获取attr的资源ID
                int attrResId = attrs.getAttributeNameResource(i);
                switch (attrResId){
                    case R.attr.customText:
                        //customText属性
                        mCustomText = attrs.getAttributeValue(i);
                        break;
                    case R.attr.customColor:
                        //customColor属性
                        //如果读取不到对应的颜色值,那么就用黑色作为默认颜色
                        mCustomColor = attrs.getAttributeIntValue(i, 0xFF000000);
                        break;
                }
            }
        }
    }

注意:xml配置只有两参的构造函数执行

方式二,使用系统主题的obtainStyledAttributes获取自定义的属性

private void init(AttributeSet attributeSet, int defStyle) {
        //首先判断attributeSet是否为null
        if(attributeSet != null){
            //获取当前MyView所在的Activity的theme
            Resources.Theme theme = getContext().getTheme();
            //通过theme的obtainStyledAttributes方法获取TypedArray对象
            TypedArray typedArray = theme.obtainStyledAttributes(attributeSet, R.styleable.MyView, 0, 0);
            //获取typedArray的长度
            int count = typedArray.getIndexCount();
            //通过for循环遍历typedArray
            for(int i = 0; i < count; i++){
                //通过typedArray的getIndex方法获取指向R.styleable中对应的属性ID
                int styledAttr = typedArray.getIndex(i);
                switch (styledAttr){
                    case R.styleable.MyView_customText:
                        //如果是R.styleable.MyView_customText,表示属性是customText
                        //通过typedArray的getString方法获取字符串值
                        mCustomText = typedArray.getString(i);
                        break;
                    case R.styleable.MyView_customColor:
                        //如果是R.styleable.MyView_customColor,表示属性是customColor
                        //通过typedArray的getColor方法获取整数类型的颜色值
                        mCustomColor = typedArray.getColor(i, 0xFF000000);
                        break;
                }
            }
            //在使用完typedArray之后,要调用recycle方法回收资源
            typedArray.recycle();
        }
    }

方式三,declare-styleable的方式获取自定义属性

自定义属性
<declare-styleable name="FrameLayoutWithHole">
        <attr name="hole_radius" format="integer" />
        <attr name="background_color" format="color" />
        <attr name="radius_x" format="integer" />
        <attr name="radius_y" format="integer" />
    </declare-styleable>
配置
<com.cdzs.tdpad.widget.FrameLayoutWithHole
            android:id="@+id/fl_circle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:visibility="invisible"
            **app:hole_radius="285"
            app:radius_y="500"/>**
   	TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FrameLayoutWithHole);
        mBackgroundColor = ta.getColor(R.styleable.FrameLayoutWithHole_background_color, -1);
        mRadius = ta.getFloat(R.styleable.FrameLayoutWithHole_hole_radius, 0);
        mRx = ta.getFloat(R.styleable.FrameLayoutWithHole_radius_x, 0);
        mRy = ta.getFloat(R.styleable.FrameLayoutWithHole_radius_y, 0);
        init(null, 0);
        ta.recycle();

方式四,获取系统主题的属性

 int[] systemeThemeAttr = new int[]{R.attr.colorPrimary,R.attr.listPreferredItemPaddingStart};
        TypedArray systemeThemeArray= getTheme().obtainStyledAttributes(systemeThemeAttr);
        Log.i("liangD","colorPrimary="+systemeThemeArray.getColor(0,100));
        Log.i("liangD","listPreferredItemPaddingStart="+systemeThemeArray.getDimension(1,100));
        systemeThemeArray.recycle();

方式五,通过代码获取自定义style中的值

  <attr name="textSize" format="dimension"></attr>
  <attr name="textString" format="string"></attr>

	<style name="TextAppearanceLargeArr" parent="father">
        <item name="textSize">19sp</item>
        <item name="textString">hello world !</item>
    </style>

 int[] attr = new int[]{R.attr.textSize,R.attr.textString};
 TypedArray array = this.obtainStyledAttributes(R.style.TextAppearanceLargeArr, attr);

  int textSize = array.getDimensionPixelSize(0  , -1);
  String textString=array.getString(1);

注意:这种方式只能每次获取最多两个属性的值,具体原因不详

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值