自定义View --- 自定义属性

【记录】记录点滴

【场景】写自定义View时,如果希望View可以支持多种样式,比如圆角ImageView可以设置圆角弧度,就需要实现自定义属性。不常写的东西容易忘。

1. /res/values/attrs.xml文件中定义属性

先附上示例代码

<!-- declare-styleable表明一个属性组,CustomView是自定义View的类名 -->
<declare-styleable name="CustomView">
        <!-- 定义一个属性,使用枚举方式 -->
        <attr name="attr1_mode">
            <enum name="mode_one" value="0"/>
            <enum name="mode_two" value="1"/>
        </attr>
        <!-- 定义一个接收String类型的属性 -->
        <attr name="attr2" format="string"/>
</declare-styleable>

2. 属性类型format

<attr>元素中 format指明了属性的类型,以常用控件为例。对于如何在自定义View中添加自定义属性,可以查看5)enum枚举类型

1) reference,如

<!-- attrs.xml定义 -->
<attr name = "background" format = "reference" />
<!-- layout.xml使用 -->
<ImageView android:background = "@drawable/drawable_id"/>

2)  color,如

<!-- attrs.xml定义 -->
<attr name = "textColor" format = "color" />
<!-- layout.xml使用 -->
<TextView android:textColor = "#00FF00" />

 3) boolean,如

<!-- attrs.xml定义 -->
<attr name = "focusable" format = "boolean" />
<!-- layout.xml使用 -->
<Button android:focusable = "true"/>

4) dimension,如

getDimension/getDimensionPixelSize/getDimensionPixelSizeOffset返回的是px值,之前一直以为是dp值。。。

<!-- attrs.xml定义 -->
<attr name = "layout_width" format = "dimension" />
<!-- layout.xml使用 -->
<Button android:layout_width = "42dp"/>

5)enum枚举,这里顺带写上,如何在自定义View中添加自定义属性的用法

<!-- 对于CustomView,在attrs.xml定义 -->
<attr name="attr1_mode">
      <enum name="mode_one" value="0"/>
      <enum name="mode_two" value="1"/>
</attr>
<!-- layout.xml使用CustomView -->
<!-- 命名空间 custom,可以根据喜好命名,例如我们可能常用就是默认的app(使用时app:xxx),引入有两种方式 -->
<!-- 方式1:xmlns:custom="http://schemas.android.com/apk/res-auto” -->
<!-- 方式2:xmlns:custom="http://schemas.android.com/apk/our_app_packagename"-->
<CustomView custom:attr1_mode="mode_one"/>

6)flag位或

flag只能是int,在java代码中通过getInt获取,并做位运算判断

<!-- attrs.xml中定义 -->
<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>
<!-- layout.xml中使用 -->
<TextView android:gravity="bottom|left"/>

7)混合类型

<!-- attrs.xml中定义 -->
<attr name = "background" format = "reference|color" />

<!-- layout.xml中使用 -->
<ImageView android:background = "@drawable/drawable_id" />
或者:
<ImageView android:background = "#00FF00" />

8)fraction

通过getFraction获取

float getFraction(int index, int base, int pbase, float defValue),如我们使用view动画时,xml中设置50%和50%p的区别

50%:原点加上自身宽/高的50%

50%p:原点加上父View宽/高的50%

<!-- attrs.xml中定义 -->
<attr name = "pivotX" format = "fraction" />
<!-- 像anim.xml中使用 -->
<rotate android:pivotX = "50%"/>

9) 其他类型,float,integer,string

3. 在Java代码中获取对应的属性

利用TypedArray获得自定义属性

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
//或者
//TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CustomView)
//enum类型的mode,假设DEFAULT_MODE=0,对应mode_one
int mode= a.getInt(R.styleable.CustomView_attr1_mode, DEFAULT_MODE);
String attr2= a.getString(R.styleable.CustomView_attr2);
//这里,用完要记得回收
a.recycle();

获取到自定义属性后,就可以绘制对应的内容了。

参考 https://www.cnblogs.com/wjtaigwh/p/6594680.html

 

补充

看了TextView的代码,也是基于上面推荐方式写的,再附上一些常用的获取方法

参考

https://blog.csdn.net/qq_27492019/article/details/78093264

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值