主要有三种方法可以实现自定义属性。

第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值。

(1)在xml文件中设置属性值

[html] view
plain
copy

  1. <com.example.activity.IconTextView
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:text="@string/smile1"
  5. iconSrc="@drawable/smile"/>

(2)在构造函数中拿到这个值

[java] view
plain
copy

  1. public IconTextView(Context context, AttributeSet attrs) {
  2. super(context, attrs);
  3. resourceID = attrs.getAttributeResourceValue(null, "iconSrc", 0);
  4. if(resourceID > 0) {
  5. bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
  6. }
  7. }

第二种方法,使用自己的命名空间

(1)注意在xml文件中,需要声明一个命名空间,形式为http:// + 这个VIEW的包名

[html] view
plain
copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:mobile="http://com.example.activity"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <com.example.activity.IconTextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/smile1"
  10. mobile:iconSrc="@drawable/smile"/>
  11. </LinearLayout>

(2)通过attrs.getAttributeResourceValue,其中第一个参数为命名空间。

[java] view
plain
copy

  1. //命名空间
  2. private final String namespace = "http://com.example.activity"
[java] view
plain
copy

  1. public IconTextView(Context context, AttributeSet attrs) {
  2. super(context, attrs);
  3. resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);
  4. //      TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
  5. //      resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);
  6. if(resourceID > 0) {
  7. bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
  8. }
  9. }

第三种方法,通过自定义attrs.xml来实现

(1)自定义一个attrs.xml文件

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="IconTextView">
  4. <attr name="iconSrc" format="reference"/>
  5. </declare-styleable>
  6. </resources>

(2)在xml文件中使用这一属性,注意此时命名空间的书写规范。

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. <com.example.activity.IconTextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/smile1"
  11. mobile:iconSrc="@drawable/smile"/>
  12. <com.example.activity.IconTextView
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="@string/smile2"
  16. android:textSize="24dp"
  17. mobile:iconSrc="@drawable/smile"/>
  18. <com.example.activity.IconTextView
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="@string/smile3"
  22. android:textSize="36dp"
  23. mobile:iconSrc="@drawable/smile"/>
  24. </LinearLayout>

(3)在代码中使用context.obtainStyledAttributes获得属性值

[java] view
plain
copy

  1. package com.example.activity;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Rect;
  8. import android.util.AttributeSet;
  9. import android.widget.TextView;
  10. public class IconTextView extends TextView {
  11. //命名空间
  12. private final String namespace = "http://com.example.activity";
  13. //资源ID
  14. private int resourceID = 0;
  15. private Bitmap bitmap;
  16. public IconTextView(Context context, AttributeSet attrs) {
  17. super(context, attrs);
  18. TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
  19. resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);
  20. if(resourceID > 0) {
  21. bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
  22. }
  23. }
  24. @Override
  25. public void onDraw(Canvas canvas) {
  26. if (bitmap != null) {
  27. Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  28. Rect target = new Rect();
  29. int textHeight = (int)getTextSize();
  30. target.left = 0;
  31. target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;
  32. target.bottom = target.top + textHeight;
  33. target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight()));
  34. canvas.drawBitmap(bitmap, src, target, getPaint());
  35. canvas.translate(target.right + 2, 0);
  36. }
  37. super.onDraw(canvas);
  38. }
  39. }

第三种方法实例实现的是一个自定义的带图片的TextView,效果图如下


VIEW当中自定义属性的使用的更多相关文章

  1. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  2. 手机安全卫士——在设置中心 自定义view和自定义属性

    自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载   ...

  3. 自定义view(13)自定义属性

    1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ...

  4. Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)

    很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...

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

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

  6. Android 手机卫士--自定义属性

    在前面的文章中,已经实现了“设置中心”第一栏的功能以及布局 本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,转载请注明出处. 自定义属性声明 接 ...

  7. Android 自定义view (一)——attr 理解

    前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ...

  8. [原] Android 自定义View步骤

    例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ...

  9. View (一)LayoutInflater()方法详解

    相信接 触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对 LayoutInflater不怎么熟悉,因为加载布 ...

随机推荐

  1. VMware vSphere(虚拟化平台)

    VMware vSphere 是业界领先且最可靠的虚拟化平台.vSphere将应用程序和操作系统从底层硬件分离出来,从而简化了 IT操作.您现有的应用程序可以看到专有资源,而您的服务器则可以作为资源池 ...

  2. 使用juqery-ui完成联想查询功能

    最近公司的项目有个需求,需要使用联想查询功能.就是一个文本输入框,在输入的时候获取值去后端模糊查询然后按照列表显示在下面.效果如下图: 经过搜索找到这个插件,查阅资料可以完成这个功能,即可以实现静态数 ...

  3. Ubuntu编译ruby

    要用sass,需要ruby2.0以上版本 ubuntu升级ruby到2.1 1.安装前更新: sudo apt-get -y update sudo apt-get install cmake sud ...

  4. Docker问题方案收集

    1.问题: Unable to connect to unix:///var/run/docker.sock (Permission denied) from PHP code 解决方法: Make ...

  5. Java8 Collections.sort()及Arrays.sort()中Lambda表达式及增强版Comparator的使用

    摘要:本文主要介绍Java8 中Arrays.sort()及Collections.sort()中Lambda表达式及增强版Comparator的使用. 不废话直接上代码 import com.goo ...

  6. Oracle 简单统计示例

    有数据如下: eg1:现在需要统计所有男性人员数量,所有女性人员数量,sclassno=10000的男性人员的总年龄,年龄大于20的女性人员数量 ----sign( number )/*If numb ...

  7. Openstack API 类型 & REST 风格

    目录 目录 Openstack 提供了三种操作方式 Web界面 CIL 指令行 RESTful API REST 风格 RESTFul风格的API设计 基于HTTP协议的RESTful API Ope ...

  8. 控件识别工具Inspect.exe下载

    一.Inspect.exe 控件识别工具.官网上说通过下载安装Windows SDK后,可以在目录C:\Program Files (x86)\Microsoft SDKs\Windows Kits\ ...

  9. Java学习之多线程(定义)

    进程:正在运行中的程序线程:负责执行程序的控制单元(执行路径)一个进程中可以有多个执行路径,称之为多线程一个进程中至少要有一个线程 创建新执行线程有两种方式 一.继承Thread类步骤:1.定义一个类 ...

  10. flink-training-course

    目录 flink-training-course 大数据领域顶级盛会 Flink Forward Asia 2019 详情