自定义属性

1.为什么要使用自定义属性?
由于需求的不满足
2.怎样自定义属性?
第一种:我们并不知道系统定义了此名称的属性,我们自己定义一个名为text或者mText的属性(属性名称可以随便起的)
<resources> <declare-styleable name="MyTextView"> <attr name=“text" format="string" /> </declare-styleable></resources>
第二种:我们知道系统已经定义过名称为text的属性,我们不用自己定义,只需要在自定义属性中申明,我要使用这个text属性(注意加上android命名空间,这样才知道使用的是系统的text属性)
<resources> <declare-styleable name="MyTextView"> <attr name=“android:text"/> </declare-styleable></resources>
3.属性值的类型format
format一共支持的类型一共有11种
(1). reference:参考某一资源ID
  • 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /></declare-styleable>
  • 属性使用:
<ImageView android:background = "@drawable/图片ID"/>
(2). color:颜色值
  • 属性定义:
<attr name = "textColor" format = "color" />
  • 属性使用:
<TextView android:textColor = "#00FF00" />
(3). boolean:布尔值
  • 属性定义:
<attr name = "focusable" format = "boolean" />
  • 属性使用:
<Button android:focusable = "true"/>
(4). dimension:尺寸值
  • 属性定义:
<attr name = "layout_width" format = "dimension" />
  • 属性使用:
<Button android:layout_width = "42dip"/>
(5). float:浮点值
  • 属性定义:
<attr name = "fromAlpha" format = "float" />
  • 属性使用:
<alpha android:fromAlpha = "1.0"/>
(6). integer:整型值
  • 属性定义:
<attr name = "framesCount" format="integer" />
  • 属性使用:
<animated-rotate android:framesCount = "12"/>
(7). string:字符串
  • 属性定义:
<attr name = "text" format = "string" />
  • 属性使用:
<TextView android:text = "我是文本"/>
(8). fraction:百分数
  • 属性定义:
<attr name = "pivotX" format = "fraction" />
  • 属性使用:
<rotate android:pivotX = "200%"/>
(9). enum:枚举值
  • 属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr></declare-styleable>
  • 属性使用:
<LinearLayout android:orientation = "vertical"></LinearLayout>
注意:枚举类型的属性在使用的过程中只能同时使用其中一个,不能 android:orientation = “horizontal|vertical"
(10). flag:位或运算
  • 属性定义:
<declare-styleable name="名称"> <attr name="gravity"> <flag name="top" value="0x30" /> <flag name="bottom" value="0x50" /> <flag name="left" value="0x03" /> <flag name="right" value="0x05" /> <flag name="center_vertical" value="0x10" /> ... </attr></declare-styleable>
  • 属性使用:
<TextView android:gravity="bottom|left"/>
注意:位运算类型的属性在使用的过程中可以使用多个值
(11). 混合类型:属性定义时可以指定多种类型值
  • 属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /></declare-styleable>
  • 属性使用:
<ImageViewandroid:background = "@drawable/图片ID" />或者:<ImageViewandroid:background = "#00FF00" />
4.类中获取属性值
如果我们自定义属性,这个属性应该去我们的应用程序包中找,所以要引入我们应用包的命名空间 xmlns:openxu="http://schemas.android.com/apk/res-auto",res-auto表示自动查找,还有一种写法 xmlns:openxu="http://schemas.android.com/apk/com.example.openxu.myview"com.example.openxu.myview为我们的应用程序包名。
5.在构造方法中获取属性值:
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); String text = ta.getString(R.styleable.MyTextView_android_text); int mTextColor = ta.getColor(R.styleable.MyTextView_mTextColor, Color.BLACK); int mTextSize = ta.getDimensionPixelSize(R.styleable.MyTextView_mTextSize, 100); ta.recycle(); //注意回收
6.PS
如果我们写了 declare-styleable,在R文件中就会生成 styleable类,这个类其实就是将每个控件的属性分组,然后记录属性的索引值,而TypedArray正好需要通过此索引值获取属性。


CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值