TypedArray的使用

TypedArray:在自定义布局文件时

TypedArray:存储资源数组的容器;

1.这一种也是我个人借阅他人;它是通过上下文获取布局文件与attrs属性;然后getxxx()获取绑定到对应的控件;

TypedArray _TypedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MyFirstCustomerView, 0, 0);

try {

mText = _TypedArray.getString(R.styleable.MyFirstCustomerView_text);

mTextColor = _TypedArray.getColor(R.styleable.MyFirstCustomerView_textColor, Color.BLACK);

mTextSize = _TypedArray.getDimensionPixelSize(R.styleable.MyFirstCustomerView_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));

}

finally {

TypedArray _TypedArray.recycle();

}

2.可以通过obtaiStyledAttributes()方法创建出来;如果不在使用了,请注意调用recycle()方法将它释放;

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.inputView);
inputIcon = typedArray.getResourceId(R.styleable.inputView_input_icon,R.drawable.myacc);
inputHint = typedArray.getNonResourceString(R.styleable.inputView_input_hint);
isPassword = typedArray.getBoolean(R.styleable.inputView_is_password,false);

getResourceId(调用该布局文件与attrs里面存放的属性匹配存储)布局文件没有设置该对应的属性时候,会按照第二个默认值或已有的值属性;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值