Android自定义属性格式
2016-01-27 23:21
561 查看
概述
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" />
相关文章推荐
- 【Android】ViewFlipper的使用
- Androidx学习笔记(30)--- 图片下载器加入缓存图片的功能
- Androidx学习笔记(29)--- 开辟子线程 解决主线程阻塞
- Android自动跳转activity
- Androidx学习笔记(27)--- 多选对话框
- Android 图标(Icon)绘制尺寸规范
- Android入门:Activity四种启动模式
- 关于Fragment之间页面跳转时的extra信息的传递
- orhanobut_logger源码分析
- android图片适配
- android访问网页
- android开发如何选择compileSdkVersion, minSdkVersion 和 targetSdkVersion
- Android 4.4 KitKat NotificationManagerService使用详解与原理分析(一)
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Android 可显示左上右下Drawable的TextView
- Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能
- Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能
- Android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android抓取各种log的方法
- Android IPC机制(四)用ContentProvider进行进程间通信