第一种方法,直接设置属性值,通过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. SGU 104

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...

  2. iOS第三方支付-微信支付

    微信支付用到的文件 1.首先支持非arc 2.设置URL types 3.AppDelegate - (BOOL)application:(UIApplication *)application di ...

  3. UITableView多选删除

    设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...

  4. poj 3621(最优比率环)

    题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...

  5. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

    一.简介 1.You now have three methods for distinguishing references:  Objects are identical if they occ ...

  6. MyBatis主键返回

    在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能. 比如在表的关联关系中,将数据插入主 ...

  7. MyBatis学习总结_01_MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  8. CentOS下判断自己的VPS是OpenVZ的还是Xen的

    一般来说,VPS的虚拟化技术,有Xen.OpenVZ.Xen HVM和VMware这几种,那么,如何判断自己的VPS是基于哪种虚拟化技术的呢? 1.执行:ls /proc/命令,一般Xen的VPS,/ ...

  9. 笔者带你剖析淘宝TDDL(TAOBAO DISTRIBUTE DATA LAYER)

    注:本文部分内容引用本人博客http://gao-xianglong.iteye.com/blog/1973591   前言 在开始讲解淘宝的TDDL(Taobao Distribute Data L ...

  10. DataGridView过滤区分大小写问题

    DataTable上的过滤方法: 一.可以用DataTable.Select("条件"),返回DataRow[]格式的结果集. DataRow[] drArr = dt.Selec ...