VIEW当中自定义属性的使用
第一种方法,直接设置属性值,通过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);
- }
- }
第三种方法实例实现的是一个自定义的带图片的TextView,效果图如下

VIEW当中自定义属性的使用的更多相关文章
- Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
		Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ... 
- 手机安全卫士——在设置中心  自定义view和自定义属性
		自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载 ... 
- 自定义view(13)自定义属性
		1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ... 
- Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)
		很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ... 
- Android初级教程初谈自定义view自定义属性
		有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ... 
- Android 手机卫士--自定义属性
		在前面的文章中,已经实现了“设置中心”第一栏的功能以及布局 本文地址:http://www.cnblogs.com/wuyudong/p/5936016.html,转载请注明出处. 自定义属性声明 接 ... 
- Android 自定义view (一)——attr 理解
		前言: 自定义view是android自定义控件的核心之一,那么在学习自定义view之前,我们先来了解下自定义view的自定义属性的attr的用法吧 Android attr 是什么 (1)attr ... 
- [原] Android 自定义View步骤
		例子如下:Android 自定义View 密码框 例子 1 良好的自定义View 易用,标准,开放. 一个设计良好的自定义view和其他设计良好的类很像.封装了某个具有易用性接口的功能组合,这些功能能 ... 
- View (一)LayoutInflater()方法详解
		相信接 触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对 LayoutInflater不怎么熟悉,因为加载布 ... 
随机推荐
- 【HDOJ6582】Path(最短路图,最小割)
			题意: n,m<=1e4,c<=1e9 思路: #include<bits/stdc++.h> using namespace std; typedef long long l ... 
- BZOJ 3772: 精神污染(dfs序+主席树)
			传送门 解题思路 比较神仙的一道题.首先计算答案时可以每条路径所包含的路径数,对于\(x,y\)这条路径,可以在\(x\)这处开个\(vector\)存\(y\),然后计算时只需要算这个路径上每个点的 ... 
- 新建工程spring boot
			新建工程spring boot 使用Maven管理, 在官网(http://atart.spring.io)下载demo后,加入依赖 <dependency> <gr ... 
- xcodebuild自动打包上传到蒲公英的shell脚本
			注意: ExportOptions.plist (包含了证书相关信息) 该plist 文件可以通过xcode手动导出ipa之后获取到, 区分appstore 和 development的情况 #! / ... 
- Java学习之多线程(线程安全问题及线程同步)
			一.线程安全问题产生前提:1.多线程操作共享数据2.线程任务中有多条代码 class Ticket implements Runnable { //2.共享数据 private int num = 1 ... 
- VMWARE ESXI 虚拟硬盘的格式:精简置备、厚置备延迟置零、厚置备置零
			精简置备(thin): 精 简配置就是无论磁盘分配多大,实际占用存储大小是现在使用的大小,即用多少算多少.当客户机有输入输出的时候,VMkernel首先分配需要的空间并进行 清零操作,也就是说如果使用 ... 
- Centos 7.3 配置Xmanager XDMCP
			我们通常需要远程桌面,这会带来很好的便利性,而Centos7的XDMCP配置过程发生了变化,添加了很多新特性,初期难免会不适应,但新系统终究还是不错的.下面看看Centos7下如何配置XManager ... 
- appium常见问题04_查看andriod内置浏览器webview版本
			方法一:手机上设置中查看 设置-->应用程序管理-->全部-->Android System WebView 方法二:adb指令查看(前提,已安装android sdk环境) 1,w ... 
- Python 学习笔记12 函数模块
			函数的优点之一,使用它们可将代码块与主程序分离.通过给函数指定描述性的名称.可以让主程序非常好理解.但是如果将过多的函数和主程序放置在一起,会让文件显得非常凌乱.太多的代码混杂在一起,不方便管理.我们 ... 
- go 语言结构控制
			if else 结构: #第一种 if condition { // do something } #第二种 if condition { // do something } else { // d ... 
