实现自定义属性的三种方法
来源:互联网 发布:淘宝动漫海报店推荐 编辑:程序博客网 时间:2024/05/16 05:07
实现自定义属性主要有三种方法
第一种方法,直接设置属性值,通过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,效果图如下
0 0
- 实现自定义属性的三种方法
- VIEW当中三种自定义属性的方法
- 自定义属性的实现
- 自定义控件三实现方法
- 三种自定义CELL 的方法
- Android自定义LinearLayout的三种方法
- 自定义UITableviewCell的三种方法
- 添加自定义MIB的三种方法
- 三种自定义UITableViewCell的方法
- Python导入自定义的三种方法
- Android自定义控件的自定义属性2种实现方式
- 自定义View属性的方法
- Struts2的Action属性名和s三种实现方法
- Java反射的三种实现方式,访问私有方法和私有属性
- Get Custom Attribute on frontend list page and cart page(列表页获取自定义属性的三种方法)
- Unity自定义ScriptableObject属性显示的三种方式
- 自定义表单的三种方式实现
- JS中使用"data-"开头的属性及data()方法实现自定义属性
- 出栈序列判定 网上一些代码虽然能AC但是有bug
- Oracle EBS诊断方面几种情况的解决方法(诊断菜单隐藏、个性化功能报错、关于此页的显示)
- 手写数字图片库MNIST百度网盘下载链接~
- gai
- 函数TH_POPUP 功能: 向函数入口参数 client , user决定的用户发送信息(信息为入口参数 message 决 定的信息)
- 实现自定义属性的三种方法
- 学生调试问题总结
- 刘涛最新封面照曝光,网友称“风格百变这很刘涛”
- 【华为 OJ 】统计大写字母个数
- Visual Studio找不到iOS模拟器
- 开发nodejs模块并发布到npm的简单示例
- HDU 2222 Keywords Search
- C#详析GetHashCode方法
- Python3.X:编写基于MySQL的程序