从AttributeSet 中取出特定的值


给定一个 AttributeSet attrs (比如在 LayoutInflater 里面,尚未创建 view 之前就已经解析出了 attrs)怎样获取特定的属性 呢:


    
    int[] attrsArray = new int[] {
        android.R.attr.id, // 0
        android.R.attr.background, // 1
        android.R.attr.layout_width, // 2
        android.R.attr.layout_height // 3
    };
    
    TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
    
    int id = ta.getResourceId(0 /* index of attribute in attrsArray */, View.NO_ID);
    Drawable background = ta.getDrawable(1);
    int layout_width = ta.getLayoutDimension(2, ViewGroup.LayoutParams.MATCH_PARENT);
    int layout_height = ta.getLayoutDimension(3, ViewGroup.LayoutParams.MATCH_PARENT);
    ta.recycle();


转载于:https://my.oschina.net/u/255456/blog/628537

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值