实现自定义属性主要有三种方法

第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值。
(1)在xml文件中设置属性值
[html]view plaincopy
<com.example.activity.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/smile1"
iconSrc="@drawable/smile"/>
(2)在构造函数中拿到这个值
[java]view plaincopy
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的包名
[html]view plaincopy
<LinearLayoutxmlns: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,其中第一个参数为命名空间。
[java]view plaincopy
//命名空间
privatefinal String namespace = "http://com.example.activity"
[java]view plaincopy
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文件
[html]view plaincopy
xmlversion="1.0"encoding="utf-8"?>
<resources>
<declare-styleablename="IconTextView">
<attrname="iconSrc"format="reference"/>
declare-styleable>
resources>
(2)在xml文件中使用这一属性,注意此时命名空间的书写规范。
[html]view plaincopy
xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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获得属性值
[java]view plaincopy
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;  
publicclass IconTextView extends TextView {  
//命名空间
privatefinal String namespace = "http://com.example.activity";  
//资源ID
privateint 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
publicvoid 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,效果图如下

转载于:https://my.oschina.net/u/3486497/blog/917436

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值