一、组件的属性

我们在布局文件中使用组件时,定义组件的宽、高、背景等属性,这些属性我们并没有特意去定义,它们都是组件的默认属性,在我们sdk安装目录下找到:

sdk\platforms\android-25\data\res\values\attrs.xml.

这里面定义了组件的默认属性,例如View的属性:

.......

.......省略

.......

二、自定义属性

在自定义组件时,我们常常需要自定义属性,自定义属性主要有3个步骤:

1、在res\values\attrs.xml 文件中定义declare-styleable标签,并将自定义的属性都定义在该标签中

(1)自定义的属性都放在declare-styleable标签中,该标签的name一般都是自定义组件的名称,此处为XView,也可以取别的名字,但是和自定义组件一个名字通俗易懂.

(2)自定义的属性由2部分组成:属性名name、属性类型format,属性类型一共有下面几种:

boolean:布尔值

color:颜色值

dimension:尺寸,比如dp、sp、px

string:字符串

float:浮点值

integer:整型值

fraction:百分数

一般在动画资源中使用,比如: 、中fromX、fromY就是fraction类型

enum:枚举值

需要在attr标签下使用标签定义枚举值,例:

flag:位或运算

需要在attr标签下使用标签定义子标签,像我们常用的gravity、layout_gravity都是该属性类型,例:

reference:引用另外一个资源

比如android:layout_width="@dimen/activity_horizontal_margin"就是引用另一个尺寸资源

PS:属性可以指定多种类型,比如我们常用的background

在布局文件中使用时值为color或者reference都可以:

android:background="#ff0000ff"

android:background="@mipmap/ic_launcher"

2、在布局文件中使用自定义属性

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

app:attr1="随风飘扬的Smile"

app:attr2="#ff0000ff" />

使用自定义属性需要导入命名空间,上面有2个命名空间:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

第1个是Android本身的,如果没有的话就不能使用组件的默认属性了,第2个是我们自定义属性的.

(1)xmlns后面的android、app是空间名称,我们在设置属性时的前缀就是这个

(2)后面的res/android、res-auto表明属性的出处,前者是android本身的,后者是AndroidStudio里面自定义属性固定的写法.

3、在自定义组件的构造方法中读取属性值

public class XView extends View {

public XView(Context context) {

this(context, null);

}

public XView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public XView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.XView);

String attr1 = a.getString(R.styleable.XView_attr1);

int attr2 = a.getColor(R.styleable.XView_attr2, 0);

a.recycle();

}

}

三、Why

上面写了如何自定义属性、如何使用,但是你肯定疑惑为什么这么写,下面就介绍上面写法的原因.

1、AttributeSet

构造方法中有个参数AttributeSet,之前介绍过,它表示属性集,我们为该组件定义的所有属性都保存在其中,拿上面的XView示例:

public class XView extends View {

public XView(Context context) {

this(context, null);

}

public XView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public XView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

for (int i = 0; i < attrs.getAttributeCount(); i++) {

String attributeName = attrs.getAttributeName(i);

String attributeValue = attrs.getAttributeValue(i);

Log.d("获取属性", "属性名" + attributeName + "------" + "属性值" + attributeValue);

}

}

}

结合Log一看就明白了,AttributeSet 中还有一些其它的方法,感兴趣的可以去看看.

2、TypedArray

(1)获取TypedArray对象

TypedArray是一个数组容器,这个容器中装有Context.obtainStyledAttributes( )方法获取到的属性值.

在获取TypedArray时,一共4个重载方法,我们来看最长的一个:

public final TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr,int defStyleRes)

set:属性集

attrs:我们定义的属性都会在R.java下生成一个id

在declare-styleable标签下的属性id又会在R.styleable下生成一个int[]类型数组

数组元素就是declare-styleable标签下所有属性的id

这里需要的就是这个int[]类型数组

defStyleAttr

defStyleRes

这两个参数涉及到默认theme,一般defStyleAttr传构造方法里面的defStyleAttr,defStyleRes传0就可以了

(2)读取属性值

获取到TypedArray对象之后,我们就可以根据TypedArray一系列getXXX( )方法获取到属性值.比如上面的:

String attr1 = a.getString(R.styleable.XView_attr1);

int attr2 = a.getColor(R.styleable.XView_attr2, 0);

TypedArray中定义了很多getXXX( )方法,XXX是我们定义的属性类型,有些方法有1个参数,有些方法有2个参数,第1个参数是索引值,第2个参数是默认值.

(3)释放资源

使用完TypedArray之后需要调用recycle( )方法释放资源.

3、declare-styleable标签

(1)在attrs.xml下所有自定义属性都会在项目的R.java中生成相应的id(R.attr中)

public final class R {

public static final class attr {

public static final int attr1=0x7f01013a;

public static final int attr2=0x7f01013b;

}

}

(2)如果这些自定义属性时定义在declare-styleable标签下,还会在R.java中生成对应int[]类型数组(R.styleable 中)

public final class R {

public static final class styleable {

public static final int[] XView = {

0x7f01013a, 0x7f01013b

};

public static final int XView_attr1 = 0;

public static final int XView_attr2 = 1;

}

}

可以看到:R.styleable 中生成了一个int[]类型数组,数组元素就是上面所有自定义属性的id

同时,数组的每个元素都有一个索引XView_attr1 、XView_attr2

(3)读取在declare-styleable标签下的自定义属性

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.XView);

String attr1 = a.getString(R.styleable.XView_attr1);

int attr2 = a.getColor(R.styleable.XView_attr2, 0);

根据int[]类型数组R.styleable.XView获取TypedArray 对象

根据数组元素索引R.styleable.XView_attr1、R.styleable.XView_attr2获取属性值

四、Demo

attrs.xml:

布局文件:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

app:text="随风飘扬的Smile"

app:textColor="#ff0000ff"

app:textSize="16sp" />

XView:

public class XView extends View {

private static final int DEFAULT_TEXT_COLOR = Color.BLACK;

private static final int DEFAULT_TEXT_SIZE = 16;

private String mText;

private int mTextColor = DEFAULT_TEXT_COLOR;

private int mTextSize = DEFAULT_TEXT_SIZE;

private Paint mPaint;

public XView(Context context) {

this(context, null);

}

public XView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public XView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

//读取属性

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.XView);

mText = a.getString(R.styleable.XView_text);

mTextColor = a.getColor(R.styleable.XView_textColor, DEFAULT_TEXT_COLOR);

mTextSize = a.getDimensionPixelSize(R.styleable.XView_textSize, DEFAULT_TEXT_SIZE);

a.recycle();

initPaint();

}

/**

* 初始化画笔

*/

private void initPaint() {

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mPaint.setTextSize(mTextSize);

mPaint.setColor(mTextColor);

mPaint.setTextAlign(Paint.Align.CENTER);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawText(mText, getMeasuredWidth() / 2, getHeight() / 2, mPaint);//绘制文本

}

}

这里自定义了一个十分简陋的“TextView”,很多东西都没有考虑,不过不要紧,这里主要是让大家明白自定义属性如何定义、使用而已.

android 自定义组件 属性值,自定义组件之自定义属性相关推荐

  1. 通过反射动态修改自定义注解属性值

    通过反射动态修改自定义注解属性值 java/lang/reflect 这个包下面都是Java的反射类和工具. Annotation 注解,也是位于这个包里的. 注解自从Java 5.0版本引入后,就成 ...

  2. 自动装配——@Autowired 构造器,参数,方法,属性都是从容器中获取参数组件的值||自定义组件想要使用Spring容器底层的一些组件 ApplicationContext,BeanFactory

    @Autowired:构造器,参数,方法,属性:都是从容器中获取参数组件的值 * 1).[标注在方法位置]:@Bean+方法参数:参数从容器中获取;默认不写@Autowired效果是一样的:都能自动装 ...

  3. react 动态添加组件属性_React的组件动态参数使用Underscore和Context来传递

    通常情况下,子组件明确地知道从props中传入的属性,甚至要对传入的属性进行限定.但父组件向子组件传递参数时,有时参数名称是作为变量出现的,无法预先明确下来,这就是动态的参数传递.动态参数传递的方法是 ...

  4. android 多个属性值,android布局属性值fill_parent和match_parent

    在编写xml的时候,如果我们想让一个控件布满父容器,可以将layout_width和layout_height的值设置为fill_parent或者是match_parent,在高一点的版本中,谷歌建议 ...

  5. 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...

  6. android自定义控件是一个 内部类 如何在xml中引用,android 自定义view属性

    android 自定义view属性 一个完美的自定义控件也可以添加xml来配置属性和风格.要实现这一点,可按照下列步骤来做: 1) 添加自定义属性到xml文件中 2) 在xml的中,指定属性的值 3) ...

  7. 自定义组件开发六 自定义组件

    概述 Android SDK 为我们提供了一套完整的组件库,数量多.功能强,涉及到方方面面,但是,我们依然看到软件市场上的每个 App 都有自己独特的东西,绝不是千遍一律的,而且也会和 IOS相互借鉴 ...

  8. 微信小程序——自定义组件的使用以及组件之间的传值

    自定义组件 创建 在项目的根目录中,鼠标右键,创建 components -> test 文件夹 在新建的 components -> test 文件夹上,鼠标右键,点击 新建 Compo ...

  9. react 子组件获取变量属性值

    刚刚遇到一个问题:子组件属性值绑定了变量,但是在子组件的componentDidMoiunt中拿到的值始终是undefinded.如下: 1 <PieInfo 2 title='有效病案' 3 ...

  10. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

最新文章

  1. 杨立昆辞Facebook人工智能实验室主任,任首席科学家
  2. mangodb 高频数据_金融分析量化系统,高频交易程序数据库通常采用哪种方式存贮数据?...
  3. Java序列化注意事项
  4. docker容器间数据共享
  5. 下拉框的value值怎么设置为变量_自绘制HT For Web ComboBox下拉框组件
  6. 笔记本电脑锁_2020年双11有哪些值得选购的笔记本电脑?(全能本/便携高性能笔记本电脑/设计本)...
  7. Metaspace泄漏排查
  8. 系列 | 高性能存储-MySQL数据库之存储过程揭秘
  9. 完全二叉树之深度问题
  10. Select()和SelectMany()的区别
  11. 小米开发版安装magisk_小米9SE不刷recovery直接安装Magisk面具的详细教程
  12. jenkins测试人员的使用
  13. Ionic4 Ionic-refresher 下拉更新
  14. 出租屋租赁系统源码带小程序
  15. Metron基础概念
  16. 基于matlab的运动模糊图像处理,基于matlab运动模糊图像处理
  17. 论文笔记目录(ver2.0)
  18. Python sum()函数
  19. 和计算机网络相关的段子,微信幽默段子 没有谁和谁一开始就很配
  20. CKH IOD选择通过CSG增强其数字批发和物联网客户体验

热门文章

  1. 三角波的傅里叶变换公式_南瓜老师的数学思维训练营 第14期 —— 三角恒等变换公式...
  2. 结构体数组和二维数组初始化和拷贝
  3. JAVA发布栅格图层_简单实现栅格布局的两种方式
  4. PS小知识(一)——建立不透明度:蒙版
  5. 公司内部邮件格式范文
  6. This request has been blocked; the content must be served over HTTPS.
  7. java 打印 xps_使用PrintTicket打印XPS,OutputColor PagesPerSheet无效
  8. java覆盖写入_java写入文件(覆盖和续写)
  9. WPS Excel将多个Excel文件合并到一个Excel文件中(sheet)
  10. 阻抗分析仪(LCR表)与矢量网络分析仪