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

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

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

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

public IconTextView(Context context, AttributeSet attrs) {
super(context, attrs);
resourceID = attrs.getAttributeResourceValue(null, "iconSrc", 0);
if(resourceID > 0) {
bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
}
}

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mobile="http://com.example.activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.example.activity.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/smile1"
mobile:iconSrc="@drawable/smile"/> </LinearLayout>

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

  1. //命名空间

private final String namespace = "http://com.example.activity"

 public IconTextView(Context context, AttributeSet attrs) {
super(context, attrs);
resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);
// TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
// resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);
if(resourceID > 0) {
bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
}
}

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

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconTextView">
<attr name="iconSrc" format="reference"/>
</declare-styleable>
</resources>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.example.activity.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/smile1"
mobile:iconSrc="@drawable/smile"/> <com.example.activity.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/smile2"
android:textSize="24dp"
mobile:iconSrc="@drawable/smile"/> <com.example.activity.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/smile3"
android:textSize="36dp"
mobile:iconSrc="@drawable/smile"/> </LinearLayout>

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

package com.example.activity;  

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView; public class IconTextView extends TextView {
//命名空间
private final String namespace = "http://com.example.activity";
//资源ID
private int resourceID = 0;
private Bitmap bitmap; public IconTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);
if(resourceID > 0) {
bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
}
} @Override
public void onDraw(Canvas canvas) {
if (bitmap != null) {
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Rect target = new Rect();
int textHeight = (int)getTextSize();
target.left = 0;
target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;
target.bottom = target.top + textHeight;
target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight())); canvas.drawBitmap(bitmap, src, target, getPaint()); canvas.translate(target.right + 2, 0);
} super.onDraw(canvas);
} }

android 自定义控件中获取属性的三种方式(转)的更多相关文章

  1. 【Struts2】Struts2获取session的三种方式

    1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = S ...

  2. android中解析文件的三种方式

    android中解析文件的三种方式     好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...

  3. Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)

    一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  4. java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))

    1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  5. Struts中的数据处理的三种方式

    Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...

  6. OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)

    OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...

  7. JS中事件绑定的三种方式

    以下是搜集的在JS中事件绑定的三种方式.   1. HTML onclick attribute     <button type="button" id="upl ...

  8. 获取Type的三种方式

    using System;using UnityEngine; public class Type_Test : MonoBehaviour{    private void Awake()    { ...

  9. java 获取时间戳的三种方式

      java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...

随机推荐

  1. Libevent详细说明

    文章来自官方文档的部分翻译:http://www.wangafu.net/~nickm/libevent-book/ 通过这部分的了解,基本上可以使用libevent的常用功能了.有时间建议直接看官方 ...

  2. 整数划分 Integer Partition(一)

    话说今天百度面试,可能是由于我表现的不太好,面试官显得有点不耐烦,说话的语气也很具有嘲讽的意思,搞得我有点不爽.Whatever,面试中有问到整数划分问题,回答这个问题过程中被面试官搞的不胜其烦,最后 ...

  3. nefu 120 梅森素数

    题意:给出p(1<p<=62),让你求Mp=2^p-1是否为梅森素数. 梅森素数:若p为素数,且Mp=2^p-1也是素数,则Mp为梅森素数.若p为合数,Mp=2^p-1一定为合数若p为素数 ...

  4. Git 10 周年之际,创始人 Linus Torvalds 访谈

    点这里 十年前的这一周,linux 内核社区面临一个根本性的挑战:他们不再能够使用他们的修复控制系统:BitKeeper,同时其他的软件配置管理遇到了对分布式系统的新需求.Linus Torvalds ...

  5. HDU 1041 Computer Transformation (简单大数)

    Computer Transformation http://acm.hdu.edu.cn/showproblem.php?pid=1041 Problem Description A sequenc ...

  6. android 启动adb

    1.命令行进入 sdk/platform-tools 2.执行命令 adb kill-server 3.执行命令 adb start-server

  7. 使用MbrFix.exe修复MBR分区表

    在卸载linux Ubuntu之前,先修复MBR,然后再删除Linux分区就可以了.而MbrFix.exe 就是这样一个Windows 修复MBR的应用程序软件,MbrFix.exe 不仅支持Wind ...

  8. CentOS 6.5下安装Zabbix 2.2.x

    操作系统:CentOS Mini 6.5 yum install httpd.x86_64 httpd-manual.x86_64 php-xml  php-mbstring mysql-server ...

  9. CSS中的长度值

    以下总结来自慕课网(依然比较浅显). 长度单位总结一下,目前比较常用到px(像素).em.% 百分比,要注意其实这三种单位都是相对单位. 1.像素 像素为什么是相对单位呢?因为像素指的是显示器上的小点 ...

  10. 由枚举模块到ring0内存结构 (分析NtQueryVirtualMemory)

    是由获得进程模块而引发的一系列的问题,首先,在ring3层下枚举进程模块有ToolHelp,Psapi,还可以通过在ntdll中获得ZwQuerySystemInformation的函数地址来枚举,其 ...