[android]对TextView控件AttributeSet attrs和int defStyle的理解

本文探讨在创建Android视图组件时,如何使用AttributeSet和defStyle参数来设置组件属性。通过Resources.Theme类的obtainStyledAttributes方法获取TypedArray,优先级为set > defStyleAttr > defStyleRes。讲解了attrs.xml文件中自定义属性的定义和使用,并介绍了R文件中styleable和attr类的作用。
摘要由CSDN通过智能技术生成

【此文章本想转载,但是转不了,就粘贴过来了】

 在通过xml文件构造view组件的时候,往往都要使用到AttributeSet和defStyle这个两个参数,例如Button组件的构造方法Button(Context ctx, AttributeSet attrs, int defStyle)中,ctx会调用obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)方法获得一个TypedArray,然后根据这个TypeArray来设置组件的属性。obtainStyledAttributes这类方法有好几个,真正的实现是Resources.Theme类,分别是:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值