Android自定义控件--自定义属性格式

概述

Android中自定义属性,attr中声明的format,即表明其格式。
其中ImageView包含background(背景)和src(前景)属性。

ImageView#attr

<declare-styleable name="ImageView">
        <!-- Sets a drawable as the content of this ImageView. -->
        <attr name="src" format="reference|color" />
    </declare-styleable>

View#attr

<declare-styleable name="View">
        <!-- A drawable to use as the background.  This can be either a reference
             to a full drawable resource (such as a PNG image, 9-patch,
             XML state list description, etc), or a solid color such as "#ff000000"
            (black). -->
        <attr name="background" format="reference|color" />
</declare-styleable>

View.java

 case com.android.internal.R.styleable.View_background:
                    background = a.getDrawable(attr);
                    break;

ImageView.java

Drawable d = a.getDrawable(com.android.internal.R.styleable.ImageView_src);
        if (d != null) {
            setImageDrawable(d);
        }

reference|color:可以是一个引用drawable的resourceId,也可以是一个color(最终会被封装为ColorDrawable)

attr#format

reference

参考某一资源id,即resourceId

<declare-styleable name="View">
        <attr name="id" format="reference" />
</declare-styleable>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
    android:text="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
case com.android.internal.R.styleable.View_id:
                    mID = a.getResourceId(attr, NO_ID);
                    break;

color

颜色值

<!-- Color of text (usually same as colorForeground). -->
    <attr name="textColor" format="reference|color" />
    <!-- Color of highlighted text. -->
    <attr name="textColorHighlight" format="reference|color" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:textColor="@color/white"
    android:textColorHighlight="@color/gray"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
 case com.android.internal.R.styleable.TextView_textColor:
                textColor = a.getColorStateList(attr);
                break;
case com.android.internal.R.styleable.TextView_textColorHighlight:
                textColorHighlight = a.getColor(attr, textColorHighlight);
                break;

boolean

布尔值

 <!-- Indicates the initial checked state of this button. -->
        <attr name="checked" format="boolean" />
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/checkbox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"
          android:focusable="false"
          android:clickable="false"
          android:duplicateParentState="true"/>
 final boolean checked = a.getBoolean(
                com.android.internal.R.styleable.CompoundButton_checked, false);
        setChecked(checked);

dimension

尺寸值

<declare-styleable name="ViewGroup_Layout">
        <attr name="layout_width" format="dimension">
            <enum name="fill_parent" value="-1" />
            <enum name="match_parent" value="-1" />
            <enum name="wrap_content" value="-2" />
        </attr>
</declare-styleable>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
 protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
            width = a.getLayoutDimension(widthAttr, "layout_width");
            height = a.getLayoutDimension(heightAttr, "layout_height");
        }

float

浮点值

<!-- alpha property of the view, as a value between 0 (completely transparent) and 1
             (completely opaque). -->
        <attr name="alpha" format="float" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:alpha="0.8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
case com.android.internal.R.styleable.View_alpha:
                    setAlpha(a.getFloat(attr, 1f));
                    break;

integer

整型值

<declare-styleable name="GridLayout">
        <attr name="rowCount" format="integer" />
        <attr name="columnCount" format="integer" />
 </declare-styleable>
<GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="5"
        android:rowCount="5"/>
private static final int ROW_COUNT = R.styleable.GridLayout_rowCount;
    private static final int COLUMN_COUNT = R.styleable.GridLayout_columnCount;

string

字符串

<declare-styleable name="TextView">
        <attr name="text" format="string" localization="suggested" />
</declare-styleable>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
case com.android.internal.R.styleable.TextView_text:
                text = a.getText(attr);
                break;

其中getText和getString,一个返回CharSequence,一个返回String

fraction

百分数,常用于定义animation

<declare-styleable name="RotateDrawable">
    <attr name="visible" />
    <attr name="fromDegrees" format="float" />
    <attr name="toDegrees" format="float" />
    <attr name="pivotX" format="fraction" />
    <attr name="pivotY" format="fraction" />
    <attr name="drawable" />
</declare-styleable>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromDegrees="0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="200%"
    android:pivotY="300%"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:toDegrees="360" />

enum

枚举值

    <declare-styleable name="LinearLayout">
        <attr name="orientation">
            <enum name="horizontal" value="0" />
            <enum name="vertical" value="1" />
        </attr>
    </declare-styleable>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" />
 int index = a.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1);
        if (index >= 0) {
            setOrientation(index);
        }

flag

位或运算

<attr name="gravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />

    </attr>
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom|right"
        android:text="@string/app_name" />
case com.android.internal.R.styleable.TextView_gravity:
                setGravity(a.getInt(attr, -1));
                break;

属性定义可以指定多种类型,如background,pivotX等

 <attr name="pivotX" format="float|fraction" />
        <attr name="pivotY" format="float|fraction" />
    <rotate
           android:fromDegrees="0"
           android:toDegrees="50"
           android:toYScale="1.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="1400" />
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值