做水果的有什么网站好,wordpress电脑访问不了,百度自建站,建设工程公司注册条件基本原理 自定义一个IconTextView类继承自TextView#xff0c;添加iconsrc属性#xff0c;表示图片。 重新onDraw方法#xff0c;将图片绘制到textVIew前面,然后将textView右移。 废话不多说了#xff0c;直接代码就明白。 package com.zb;import android.content.Context;…基本原理 自定义一个IconTextView类继承自TextView添加iconsrc属性表示图片。 重新onDraw方法将图片绘制到textVIew前面,然后将textView右移。 废话不多说了直接代码就明白。 package com.zb;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.util.AttributeSet;import android.view.View;import android.widget.TextView;public class IconTextView extends TextView {private final String nameSpacehttp://com.zb.text;//保存图像资源ID的变量 private int resourceId0;private Bitmap bitmap;public IconTextView(Context context, AttributeSet attrs) {super(context, attrs); resourceIdattrs.getAttributeResourceValue(nameSpace, iconSrc, 0);//获取图像资源的值 if(resourceId!0) bitmapBitmapFactory.decodeResource(getResources(), resourceId); } Overrideprotected void onDraw(Canvas canvas) {if(bitmap!null){ Rect srcnew Rect();//原图区域 Rect targetnew Rect();//目标区域 src.left0; src.top0; src.rightbitmap.getWidth(); src.bottombitmap.getHeight();int textHeight(int) getTextSize(); target.left0;//计算图像复制区域的纵坐标 target.top(int) (((getMeasuredHeight()-getTextSize())/2)1); target.bottomtarget.toptextHeight; target.right(int) (textHeight*((float)bitmap.getWidth()/bitmap.getHeight()));//绘制 canvas.drawBitmap(bitmap, src, target, getPaint());//向右移动TextView的的距离 canvas.translate(target.right2,0); }super.onDraw(canvas); }} ?xml version1.0 encodingutf-8?LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:zbhttp://com.zb.text android:layout_widthfill_parent android:layout_heightfill_parent android:orientationvertical com.zb.IconTextViewandroid:idid/iconText1 android:layout_widthfill_parent android:layout_heightwrap_content zb:iconSrcdrawable/small android:text妞给爷笑一个 /com.zb.IconTextViewandroid:idid/iconText1 android:layout_widthfill_parent android:layout_heightwrap_content zb:iconSrcdrawable/small android:textSize30dp android:text妞给爷笑一个 //LinearLayout 代码很简单把。这里需要几个地方namespace xmlns:zbhttp://com.zb.text要和代码里面定义的一样。原文http://blog.csdn.net/hopezhangbo/article/details/7351290#转载于:https://www.cnblogs.com/shanzei/archive/2012/04/06/2434500.html