自定义View的基础(二)- 自定义属性

用来记录,好记性不如烂笔头!!!

实际上自定义属性已经很多人写过,但是自己知道的,才是自己的!

在自定义view,经常要重新定义自己的属性:

  
<attr name="id" format="reference" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color"/>
<attr name="text" format="string"/>
<attr name="singleLine" format="boolean"/>
<attr name="alpha" format="float"/>
<attr name="maxLines" format="integer" min="0"/>
<attr name="paddingLeft" format="dimension"/>
<attr name="textStyle">
    <flag name="normal" value="0"/>
    <flag name="bold" value="1"/>
    <flag name="italic" value="2"/>
</attr>
<attr name="visibility">
    <enum name="visible" value="0"/>
    <enum name="invisible" value="1"/>
    <enum name="gone" value="2"/>
</attr>
<attr name="centerX" format="fraction"/>

format 一共有10种,下面会简单描述下

<TextView
    android:id="@+id/kindergarten"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:alpha="0.8f"
    android:background="@color/colorAccent"
    android:maxLines="1"
    android:padding="10dp"
    android:singleLine="true"
    android:text="kindergarten"
    android:textColor="@color/colorPrimary"
    android:textSize="14sp"
    android:textStyle="bold"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />
但是看源码中可能会发现混合类型:属性定义时可以指定多种类型值,如:

<attr name="centerX" format="float|fraction" />
<!-- Y-position of the center point of the gradient within the shape as a fraction of the
     height. The default value is 0.5. -->
<attr name="centerY" format="float|fraction" />
<!-- Radius of the gradient, used only with radial gradient. May be an explicit dimension
     or a fractional value relative to the shape's minimum dimension. -->
<attr name="gradientRadius" format="float|fraction|dimension" />

1)reference:资源id,好吧,这个示例不怎么好......

属性定义:

<attr name="id" format="reference"/>

属性使用:

android:id="@+id/kindergarten"

2)color: 颜色值

<attr name="textColor" format="color"/>

属性使用:

android:textColor="#3F51B5"

3)string  字符串

<attr name="text" format="string"/>

属性使用:

android:text="kindergarten"

4) boolean 布尔值

<attr name="singleLine" format="boolean"/>

属性使用:

<attr name="singleLine" format="boolean"/>

5)float

<attr name="alpha" format="float"/>

属性使用:

android:alpha="0.8f"

6)integer

<attr name="maxLines" format="integer" min="0"/>

属性使用:

android:maxLines="1"

7) dimension 尺寸值

<attr name="paddingLeft" format="dimension"/>

属性使用:

android:paddingLeft="10dp"

8)enum 枚举

<attr name="visibility">
    <enum name="visible" value="0"/>
    <enum name="invisible" value="1"/>
    <enum name="gone" value="2"/>
</attr>

属性使用:

android:visibility="visible"

9)flag

<attr name="textStyle">
    <flag name="normal" value="0"/>
    <flag name="bold" value="1"/>
    <flag name="italic" value="2"/>
</attr>

属性使用:

android:textStyle="bold"

10)fraction

<attr name="centerX" format="fraction"/>

属性使用

<rotate android:pivotX = "200%"/>

来个示例:

先在res中新建一个文件resource.xml文件 attrs.xml

<attr name="textSize" format="reference"/>
<attr name="textColor" format="color"/>
<attr name="text" format="string"/>
<declare-styleable name="SecurityCode">
    <attr name="text"/>
    <attr name="textSize"/>
    <attr name="textColor"/>
</declare-styleable>

在代码中体现:

//        context.getTheme().obtainStyledAttributes和context.obtainStyledAttributes没有区别
//        TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.SecurityCode, defStyleAttr, 0 );
        TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.SecurityCode, defStyleAttr, 0 );
        int n = a.getIndexCount();
        for ( int i = 0; i < n; i++ ) {
            int attr = a.getIndex( i );
            switch ( attr ) {
                case R.styleable.SecurityCode_text:
                    text = a.getString( attr );
                    break;
                case R.styleable.SecurityCode_textSize:
                    textSize = a.getDimensionPixelSize( attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics() ) );
                    break;
                case R.styleable.SecurityCode_textColor:
                    textColor = a.getColor( attr, Color.BLACK );
                    break;
            }
        }
        a.recycle();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值