android 自定义控件中获取属性的三种方式(转)
第一种方法,直接设置属性值,通过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,其中第一个参数为命名空间。
- //命名空间
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 自定义控件中获取属性的三种方式(转)的更多相关文章
- 【Struts2】Struts2获取session的三种方式
1.Map<String,Object> map = ActionContext.getContext().getSession(); 2.HttpSession session = S ...
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)
一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))
1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- Struts中的数据处理的三种方式
Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...
- OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...
- JS中事件绑定的三种方式
以下是搜集的在JS中事件绑定的三种方式. 1. HTML onclick attribute <button type="button" id="upl ...
- 获取Type的三种方式
using System;using UnityEngine; public class Type_Test : MonoBehaviour{ private void Awake() { ...
- java 获取时间戳的三种方式
java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...
随机推荐
- Integral类型的跨平台使用
fundamental integral types or extended integral types 我们先通过下图,来了解可以跨平台使用的整数类型: 之所以我们需要以上各种明确指定宽度的int ...
- 利用securecrt在linux与windows之间传输文件
SecureCRT这款SSH客户端软件同时具备了终端仿真器和文件传输功能.比ftp命令方便多了,而且服务器不用再开FTP服务了.rz,sz是便是Linux/Unix同Windows进行ZModem文件 ...
- NTP服务及时间同步(CentOS6.x)
博客分类: linux 今有一小型项目,完全自主弄,原来以为很简单的NTP服务,我给折腾了2个多小时才整撑头(以前都是运维搞,没太注意,所以这技术的东西,在简单都需要亲尝啊),这里记录为以后别再浪 ...
- hive-学习笔记
1.hive模糊搜索表 show tables like '*name*'; 2.查看表结构信息 desc formatted table_name; desc table_name; 3.查看 ...
- intellij idea搭建ssh开发框架之绑定数据源
原文:intellij idea搭建ssh开发框架之绑定数据源 在intellij idea中绑定数据源并生成hibernate实体对象.在IDE中的右边找到Database标签. 点击弹出窗口中的图 ...
- linux shell 命令学习(5) xxd- make a hexdump or do the reverse.
对于标准输入或者给定的文件,显示其16进制的内容.也可以反过来进行转换. xxd -h[elp] xxd [options] [infile [outfile]] xxd -r[evert] [opt ...
- swift:入门知识之枚举和结构体
枚举: swift中的枚举有些类似于类这个概念,它有自己的属性,也可以有自己的方法 枚举中的成员有原始值和实际值之分,原始值用来枚举成员的排序次序,默认从0开始 枚举出来的成员值就是实际值 可以通过t ...
- 14_把文件存放在SDCard
权限添加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> /** ...
- 从SDE库文件手工删除SDE图层(转载)
转载自:http://gis-conquer.blog.sohu.com/164467560.html 一.前言 虽然Catalog能解决这种问题,但是在特殊情况下也许这种方法有点用途. ...
- SQLite学习手册(内置函数)
一.聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了.这里还需要进一步说明的是,对于所有聚合函数而言, ...