Android 自定义控件属性format详解

前言

在自定义属性时,需要在declare-styable节点下添加attr,名称以name定义,类型以format定义。

1. format取值

属性说明
reference引用值
color颜色值
boolean布尔值
dimension尺寸值
float浮点值
integer整型值
string字符串
fraction百分数值
enum枚举
flag

2. 自定义属性

attrs.xml文件,自定义AttrFormatTextView,使用所有类型的属性。

<declare-styleable name="AttrFormatTextView">
    <attr name="attrReference" format="reference" />
    <attr name="attrColor" format="color" />
    <attr name="attrBoolean" format="boolean" />
    <attr name="attrDimension" format="dimension" />
    <attr name="attrFloat" format="float" />
    <attr name="attrInteger" format="integer" />
    <attr name="attrString" format="string" />
    <attr name="attrFraction" format="fraction" />
    <attr name="attrEnum" >
        <enum name="east" value="1" />
        <enum name="west" value="2" />
        <enum name="south" value="3" />
        <enum name="north" value="4" />
    </attr>
    <attr name="attrFlag">
        <flag name="top" value="0x01" />
        <flag name="bottom" value="0x02" />
        <flag name="left" value="0x10" />
        <flag name="right" value="0x20" />
    </attr>
</declare-styleable>

自定义控件AttrFormatTextView

public class AttrFormatTextView extends TextView {

    public AttrFormatTextView(Context context) {
        this(context, null);
    }

    public AttrFormatTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AttrFormatTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrFormatTextView);
        int refValue = a.getResourceId(R.styleable.AttrFormatTextView_attrReference, 0);
        int colorValue = a.getColor(R.styleable.AttrFormatTextView_attrColor, 0);
        boolean boolValue = a.getBoolean(R.styleable.AttrFormatTextView_attrBoolean, false);
        int dimenValue = a.getDimensionPixelSize(R.styleable.AttrFormatTextView_attrDimension, 0);
        float floatValue = a.getFloat(R.styleable.AttrFormatTextView_attrFloat, 0);
        int intValue = a.getInteger(R.styleable.AttrFormatTextView_attrInteger, 0);
        String strValue = a.getString(R.styleable.AttrFormatTextView_attrString);
        float fractionValue = a.getFraction(R.styleable.AttrFormatTextView_attrFraction, 100, 200, 0);
        int enumValue = a.getInt(R.styleable.AttrFormatTextView_attrEnum, 0);
        int flagValue = a.getInt(R.styleable.AttrFormatTextView_attrFlag, 0);
        a.recycle();

        if (colorValue != 0) {
            setTextColor(colorValue);
        }

        String text = (refValue != 0 ? "ref = " + getResources().getString(refValue) + "\n" : "")                
                + (boolValue ? "bool = true\n" : "")
                + (dimenValue != 0 ? "dimen = " + dimenValue + "\n" : "")
                + (floatValue != 0 ? "float = " + floatValue + "\n" : "")
                + (intValue != 0 ? "integer = " + intValue + "\n" : "")
                + (strValue != null ? "string = " + strValue + "\n" : "")
                + (fractionValue != 0 ? "fraction = " + fractionValue + "\n" : "")
                + (enumValue != 0 ? "enum = " + enumValue + "\n" : "")
                + (flagValue != 0 ? "flag = " + getFlagValue(flagValue) + "\n" : "");

        setText(text);
    }

    private String getFlagValue(int flag) {
        String flagValue = "";
        if ((flag & 0x01) != 0) {
            flagValue += "Top";
        }
        if ((flag & 0x02) != 0) {
            flagValue += flagValue.length() == 0 ? "" : " | ";
            flagValue += "Bottom";
        }
        if ((flag & 0x10) != 0) {
            flagValue += flagValue.length() == 0 ? "" : " | ";
            flagValue += "Left";
        }
        if ((flag & 0x20) != 0) {
            flagValue += flagValue.length() == 0 ? "" : " | ";
            flagValue += "Right";
        }
        return flagValue;
    }
}

在布局文件中,分别使用各种类型数据

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.blog.demo.custom.widget.AttrFormatTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:attrReference="@string/app_name"
        app:attrColor="#ffff0000"
        app:attrBoolean="true"
        app:attrDimension="15dp"
        app:attrFloat="1.43"
        app:attrInteger="36"
        app:attrString="This is a String"
        app:attrFraction="50%"
        app:attrEnum="south"
        app:attrFlag="top|left" />

</LinearLayout>

效果如下
在这里插入图片描述

3. fraction类型

fraction百分数值有两种方式,分别是50%50%p

在方法getFraction(int index, int base, int pbase, float defValue)方法中,如果值是50%,使用base*0.5。一旦定义为50%p,则使用pbase*0.5

<com.blog.demo.custom.widget.AttrFormatTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:attrFraction="50%p" />

效果如下
在这里插入图片描述

4. enum和flag类型

这两个类型都使用int值来计算,区别在于enum只能选择某个值,而flag可以选择多个值。

<com.blog.demo.custom.widget.AttrFormatTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:attrEnum="east"
    app:attrFlag="bottom|right"/>

效果如下
在这里插入图片描述

相关文章
Android 自定义控件属性
Android 自定义控件属性format详解
Android 自定义控件属性赋值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值