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

    平衡树总是有用的,set由于过度封装没有办法实现找比x小的元素有多少个,这就显得很不方便了,所以封装了个Treap,万一以后用的着呢- -01 #pragma warning(disable:4996 ...

  2. Thread类详解

    java.lang 类 Thread java.lang.Object java.lang.Thread 所有已实现的接口: Runnable public class Threadextends O ...

  3. LoaderManager使用详解(四)---实例:AppListLoader

    实例:AppListLoader   这篇文章将是我的第四篇,也就是最后一篇该系列的文章.请在评论里面告诉我他们是否有用.前面几篇文章的链接如下:   一:Loaders之前世界 二:了解Loader ...

  4. App接口设计

    关于APP接口设计 http://blog.csdn.net/gebitan505/article/details/37924711/

  5. tcp抓包 Wireshark 使用

    fidder主要是针对http(s)协议进行抓包分析的,所以类似wireshark/tcpdump这种工作在tcp/ip层上的抓包工具不太一样,这种工具一般在chrome/firefox的开发者工具下 ...

  6. iOS 开发--转场动画

    "用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同 ...

  7. Centos环境下部署游戏服务器-Eclipse

    一直在想这篇文章该不该写,因为这篇文章更像是教你如何使用一个ide这种文章,毫无价值可言.但思来想去还是应给写.上篇文章主要说了编译原理和过程,这篇文章就是理论联系实际的典范.并且很多工程师一辈子都不 ...

  8. 3、Object对象的两大方法(hashCode-equals)总结

    Object类是所有java类的父类. 用户定义了如下一个Person类 public class Person{} 在类定义中并没有明确继承Object类,但是编译器会自动的完成这个过程. 既然所有 ...

  9. adb 安卓opencv manager报错:adb server is out of date.killing

    原因:ref:http://jingyan.baidu.com/article/d621e8da0dee022865913fce.html      最后发现360mobil.exe占用 5037 通 ...

  10. Session与Cookie

    Session与Cookie的比较 Cookie与Session都可以进行会话跟踪,但是实现的原理不太一样.一般情况下二者均可以满足需求,但有时候不可以使用Cookie,有时候不可以使用Session ...