• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

android 设计带图片的文本框代码

Android 水墨上仙 1824次浏览

android 设计带图片的文本框代码
基本原理
自定义一个IconTextView类继承自TextView,添加iconsrc属性,表示图片。
重新onDraw方法,将图片绘制到textVIew前面,然后将textView右移。
废话不多说了,直接代码就明白。
转自:http://blog.csdn.net/hopezhangbo/article/details/7351290

<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>  </span></span></li></ol></div><pre name="code" class="java" style="background-color: rgb(255, 255, 255); display: none;">

[java] view plaincopyprint?

  1. package com.zb;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Rect;
  7. import android.graphics.drawable.BitmapDrawable;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. import android.widget.TextView;
  11. public class IconTextView extends TextView {
  12. private final String nameSpace=“http://com.zb.text”;
  13. //保存图像资源ID的变量
  14. private int resourceId=0;
  15. private Bitmap bitmap;
  16. public IconTextView(Context context, AttributeSet attrs) {
  17. super(context, attrs);
  18. resourceId=attrs.getAttributeResourceValue(nameSpace, “iconSrc”, 0);//获取图像资源的值
  19. if(resourceId!=0)
  20. bitmap=BitmapFactory.decodeResource(getResources(), resourceId);
  21. }
  22. @Override
  23. protected void onDraw(Canvas canvas) {
  24. if(bitmap!=null){
  25. Rect src=new Rect();//原图区域
  26. Rect target=new Rect();//目标区域
  27. src.left=0;
  28. src.top=0;
  29. src.right=bitmap.getWidth();
  30. src.bottom=bitmap.getHeight();
  31. int textHeight=(int) getTextSize();
  32. target.left=0;
  33. //计算图像复制区域的纵坐标,
  34. target.top=(int) (((getMeasuredHeight()-getTextSize())/2)+1);
  35. target.bottom=target.top+textHeight;
  36. target.right=(int) (textHeight*((float)bitmap.getWidth()/bitmap.getHeight()));
  37. //绘制
  38. canvas.drawBitmap(bitmap, src, target, getPaint());
  39. //向右移动TextView的的距离
  40. canvas.translate(target.right+2,0);
  41. }
  42. super.onDraw(canvas);
  43. }
  44. }
  1. "code" class="plain">"1.0" encoding="utf-8"?>  
  2. "http://schemas.android.com/apk/res/android"
  3. xmlns:zb="http://com.zb.text"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. android:id="@+id/iconText1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. zb:iconSrc="@drawable/small"
  11. android:text="妞给爷笑一个" />
  12. android:id="@+id/iconText1"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. zb:iconSrc="@drawable/small"
  16. android:textSize="30dp"
  17. android:text="妞给爷笑一个" />
  18. "code" class="java" style="background-color: rgb(255, 255, 255); ">
    "code" class="java" style="background-color: rgb(255, 255, 255); ">
    "code" class="java" style="background-color: rgb(255, 255, 255); ">"font-family: monospace; white-space: pre; ">代码很简单把。  
  19. "code" class="java" style="background-color: rgb(255, 255, 255); ">布局文件
    "code" class="java" style="background-color: rgb(255, 255, 255); ">
    "code" class="java" style="background-color: rgb(255, 255, 255); ">这里需要几个地方,namespace xmlns:zb="http://com.zb.text"要和代码里面定义的一样。
    "code" class="java" style="background-color: rgb(255, 255, 255); ">ok!!

  20. 
    

  21. 
    

  22. 
    

  23. 
    

  24. 
    

  25. 
    

  26. 
    

  27. 
    

  28. 
    

  29. 
    

"code" class="java" style="background-color: rgb(255, 255, 255); ">布局文件
"code" class="java" style="background-color: rgb(255, 255, 255); ">
"code" class="java" style="background-color: rgb(255, 255, 255); ">这里需要几个地方,namespace xmlns:zb="http://com.zb.text"要和代码里面定义的一样。
"code" class="java" style="background-color: rgb(255, 255, 255); ">ok!!

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • "code" class="java" style="background-color: rgb(255, 255, 255); ">布局文件
    "code" class="java" style="background-color: rgb(255, 255, 255); ">
    "code" class="java" style="background-color: rgb(255, 255, 255); ">这里需要几个地方,namespace xmlns:zb="http://com.zb.text"要和代码里面定义的一样。
    "code" class="java" style="background-color: rgb(255, 255, 255); ">ok!!

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 
    

  • 加载中……