1. public class DrawableTopLeftTextView extends TextView {
  2. 	private Paint mPaint;
  3. 	private float fFontHeight;
  4. 	private Drawable[] drawables;
  5. 	private int leftMargin = 40; //TODO 这个要通过代码获取,不能硬编码
  6. 	public DrawableTopLeftTextView(Context context, AttributeSet attrs, int defStyle) {
  7. 		super(context, attrs, defStyle);
  8. 		init();
  9. 	}
  10. 	public DrawableTopLeftTextView(Context context, AttributeSet attrs) {
  11. 		super(context, attrs);
  12. 		init();
  13. 	}
  14. 	public DrawableTopLeftTextView(Context context) {
  15. 		super(context);
  16. 		init();
  17. 	}
  18. 	private void init() {
  19. 		mPaint = getPaint();
  20. 		//mPaint.setTextSize(56.0f);
  21. 		mPaint.setColor(Color.parseColor("#cccccccc"));
  22. 		FontMetrics fm = mPaint.getFontMetrics();
  23. 		fFontHeight = (float) Math.ceil(fm.descent - fm.ascent);
  24. 		drawables = getCompoundDrawables();
  25. 	}
  26. 	@Override
  27. 	protected void onDraw(Canvas canvas) {
  28. 		if (drawables != null) {
  29. 			Drawable drawable = drawables[1]; // top
  30. 			if (drawable != null) {
  31. 				final float textY = getY() + drawable.getIntrinsicHeight() + fFontHeight - 5;
  32. 				canvas.drawText(getText().toString(), 0, textY, mPaint);
  33. 				canvas.clipRect(drawable.getBounds());
  34. 				canvas.drawBitmap(getBitmap(drawable), getLeft()+leftMargin, getTop(), mPaint);
  35. 				drawable.draw(canvas);
  36. 				canvas.save();
  37. 			}
  38. 		}
  39. 		super.onDraw(canvas);
  40. 	}
  41. 	private final static Bitmap getBitmap(Drawable drawable){
  42. 		BitmapDrawable bd = (BitmapDrawable) drawable;
  43. 		return bd.getBitmap();
  44. 	}
  45. }
  46. <com.bluetooth.light.widget.DrawableTopLeftTextView
  47.             android:id="@+id/signal_text"
  48.             android:layout_width="wrap_content"
  49.             android:layout_height="wrap_content"
  50.             android:layout_alignParentRight="true"
  51.             android:layout_marginRight="10dp"
  52.             android:drawableTop="@drawable/signal_logo"
  53.             android:text="@string/signal"
  54.             android:textColor="#cccccccc"
  55.             android:textSize="24sp" />
  56. 效果图

Android 自己定义 TextView drawableTop 图标与文字左对齐(效果图)的更多相关文章

  1. day3-3种实现小图标与文字水平对齐的方式

    效果图: 1.使用小图标作为背景图实现 html: <div class="test"> <ul> <li class="method1&q ...

  2. css实现一行文字居中,多行文字左对齐

    问题及场景: 当内容能一行显示在盒子内时,文字居中对齐. 当内容过多换行后显示在盒子内时,文字左对齐. 其实这种视觉上的需求还是蛮常见的.比如用于弹出提示框,当提示内容比较少时,内容居中显示在弹出框, ...

  3. android Editview中加小图标或者文字实现

    关于这个问题,如果只是加小图标的话,已经提供了很好的支持,drawableLeft属性就可以设置左边的小图标,类推,右边也可以 不过如果你要加的是文字,我找了下,没有相应的属性,我们只能通过转换思路去 ...

  4. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  5. android 自己定义TextView&quot;会发脾气的TextView&quot;

    转载请注明出处王亟亟的大牛路 Git上看到的一个自己定义控件就搞来研究研究.蛮可爱的. 项目结构: 执行效果:非常Q谈.谈的图片什么都 都能够换哦 自己定义View: public class Jel ...

  6. Android 自己定义TextView 实现文本间距

    转载请标明出处: http://blog.csdn.net/u011974987/article/details/50845269: Android系统中TextView默认显示中文时会比較紧凑.不是 ...

  7. android自己定义TextView

    Android控件中的TextView控件仅仅有一个输入框.可是为了用于的操作方便我们应该实现一些功能: 1. 能够直接将内容删除的功能button 2. 可以记录用户曾经输入的数据,同一时候可以将数 ...

  8. html 图标和文字一行对齐

    原图: 效果图: 备注:vertical-align:middle <div> <p class="time tl-size12" style="pad ...

  9. UIButton 按钮文字左对齐

    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; btn.titleEdgeInsets = UIEd ...

随机推荐

  1. curl 浏览器模拟请求实战

    1,curl 常用选项

  2. Unity扩展让枚举视图中变成多选框

    如图: 定义属性描述特性(因为没有描述的数据,让绘制类去绘制所以为空) using UnityEngine; using System.Collections; public class EnumFl ...

  3. 数据连接池——JNDI

    数据库连接有很多中方式,JDBC数据库的连接方式,前边我们已经介绍过了,而开发中我们经常使用的是DataBaseConnectionPool(数据库连接池,DBCP).数据库连接池到底是什么?它比jd ...

  4. Qt 信号与槽

    Qt信号与槽的理解 信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重 ...

  5. url 的httppost 和http get ,put,delect

    URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源 ,而HTTP中的GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作 .到这里 ...

  6. 如何在Google Map中处理大量标记(ASP.NET)(转)

    如何在Google Map中处理大量标记(ASP.NET)(原创-翻译) Posted on 2010-07-29 22:04 Happy Coding 阅读(8827) 评论(8) 编辑 收藏 在你 ...

  7. struts1:Struts的中央控制器

    在Struts框架中,有多个组件分担控制器的工作,它们分别是ActionServlet类,RequestProcessor类和Action类等,其中ActionServlet是Struts框架中的核心 ...

  8. codeforces #332 div2

    A. #include<cstdio> #include<algorithm> #include<cmath> #include<map> #inclu ...

  9. C语言-getopt函数

    #include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...

  10. Java API设计CheckList

    API设计原则:正确.好名.易用.易学.够快.够小.但我们从来不缺原则,〜〜〜 Interface 1.The Importance of Being Use Case Oriented,一个接口应当 ...