自定义控件0

自定义控件的最简单方法就是继承这个控件。

这次我们来重写一个TextView,让系统自带textview的左边加上一个图标。

效果如下:

实现方法:

package com.xys.widget;

import android.content.Context;
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.xys";
	//资源ID
	private int resourceID=0;
	private Bitmap bitmap;

	public IconTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		//该方法用于获得该控件属性的值
		resourceID=attrs.getAttributeResourceValue(namespace, "iconSrc",0);
		if(resourceID>0){
			bitmap=BitmapFactory.decodeResource(getResources(), resourceID);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		if(bitmap!=null){
			//从原图上截取图像区域  本例为原图
			Rect src=new Rect();
			//复制到目标区域
			Rect target=new Rect();
			src.left=0;
			src.top=0;
			src.right=bitmap.getWidth();
			src.bottom=bitmap.getHeight();
			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());
			//将textview右移2像素
			canvas.translate(target.right+2, 0);
		}
		//父类的onDrow一定要在translate方法后执行
		super.onDraw(canvas);
	}
}

布局文件中的使用方法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mobile="http://com.xys"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world">
    </TextView>

    <com.xys.widget.IconTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="IconTextView"
        mobile:iconSrc="@drawable/folder" >

    </com.xys.widget.IconTextView>

</RelativeLayout>
时间: 2024-10-04 01:54:51

自定义控件0的相关文章

Asp.net2.0之自定义控件ImageButton

上个星期三开始学自定义控件,做了不少练习.花了一上午时间写了一个imageButton,以前就像写这个控件,只是不会. 图片 正文 这个控件模仿winform中的button,可以支持图片和文字.可以选择执行服务器端程序还是客户端程序,还有一些简单的设置. 不足的是不支持样式,下次希望可以写一个工具条. 以下就是代码 以下为引用的内容: using System; using System.Collections.Generic; using System.Linq; using System.

ASP.Net2.0中自定义控件在page中的注册

asp.net|控件 今天在网上看到ASP.Net 2.0中注册自定义控件的好方法,记录如下. 在web.config 文件中全局注册自定义控件 <system.web>      <pages>        <controls>          <add tagPrefix="rx" assembly="HYLQ.Component" namespace="HYLQ.Component"/> 

ASP.net 2.0 自定义控件的开发之数据分页 第一章

asp.net|分页|控件|数据  本文将向大家介绍使用ASP.net 2.0 开发自定义的数据分页控件 数据分页的好处 1.减少网络流量 2.提高运行效率在大数据量时,数据分页是一个必须面对的现实,没有哪个客户愿意花上几分钟或更多的时间来等待.数据分页也有多种方式比如内存分页 SQL Server 存储过程分页等,这里将向大家介绍的是 SQL Server 存储过程分页方式,并结合ASP.net 2.0 的来编写一个通用的数据分页控件.本文使用的存储过程来自 http://www.codepr

IIS 7.0编辑页面和配置自定义控件的设置

http://www.aliyun.com/zixun/aggregation/14156.html">ASP.NET 网页包括一些它在运行时可由 ASP.NET 识别并处理的额外元素.ASP.NET 页面还可以包含可重用的自定义控件.这些自定义控件将由服务器处理.这样便可以使用服务器代码来设置 ASP.NET 网页属性. IIS 7.0 允许配置以下 ASP.NET 页面和用户控件设置: - 行为设置:例如,在当前页面请求结束时,该网页是否保留自身及其包含的所有服务器控件的视图状态. -

ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性

原文:ASP.NET2.0自定义控件组件开发 第六章 深入讲解控件的属性                                         深入讲解控件的属性持久化(一) 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第三篇 ASP.NET自定义控件组件开发 第二章 继承WebControl的自定义控件 ASP.NET自定义控件组件开发 第三章 为控件添加事件 前

[翻译]开发Silverlight 2.0的自定义控件

原文:Developing a Custom Control for Silverlight 2.0 译者:张善友  Download MediaButton_demo - 131.06 KB Download MediaButton_src - 10.91 KB 介绍 这篇文章主要展示了你制作一个Silverlight 2.0的自定义空间需要哪些步骤和在你的Silverlight项目中重用代码. 本文基于Mix08上发布的Silverlight 2.0 beta1. 文章中我会创建一个控件库和

ASP.NET2.0服务器控件之创建自定义控件

asp.net|创建|服务器|控件 摘要 本文将详细讲解一个简单的自定义服务器控件的创建过程.通过这些内容,读者将了解利用Visual Studio 2005,创建和测试自定义服务器控件的基本方法. 注意:本文内容基于ASP.NET 2.0技术撰写,示例应用程序使用Visual Studio 2005进行开发. 创建一个简单的自定义服务器控件 创建自定义服务器控件的过程包括: (1)创建一个测试用Web站点应用程序: (2)为新建站点应用程序,新增一个Web控件库项目: (3)编写.编译和测试自

ASP.NET 2.0服务器控件之创建自定义控件

摘要 本文将详细讲解一个简单的自定义服务器控件的创建过程.通过这些内容,读者将了解利用Visual Studio 2005,创建和测试自定义服务器控件的基本方法 . 注意:本文内容基于ASP.NET 2.0技术撰写,示例应用程序使用Visual Studio 2005进行开发. 创建一个简单的自定义服务器控件 创建自定义服务器控件的过程包括: (1)创建一个测试用Web站点应用程序: (2)为新建站点应用程序,新增一个Web控件库项目: (3)编写.编译和测试自定义服务器控件. (1)创建一个测

介绍几个好用的android自定义控件

首先看效果图, 看下这两个界面,第一个中用到了一个自定义的FlowRadioGroup,支持复合子控件,自定义布局: 第二个界面中看到了输入的数字 自动4位分割了吧:也用到了自定义的DivisionEditText控件. 下面直接看源码FlowRadioGroup了: 1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0