public class DrawableTopLeftTextView extends TextView {

	private Paint mPaint;
private float fFontHeight;
private Drawable[] drawables;
private int leftMargin = 40; //TODO 这个要通过代码获取,不能硬编码 public DrawableTopLeftTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public DrawableTopLeftTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public DrawableTopLeftTextView(Context context) {
super(context);
init();
} private void init() {
mPaint = getPaint();
//mPaint.setTextSize(56.0f);
mPaint.setColor(Color.parseColor("#cccccccc"));
FontMetrics fm = mPaint.getFontMetrics();
fFontHeight = (float) Math.ceil(fm.descent - fm.ascent);
drawables = getCompoundDrawables();
} @Override
protected void onDraw(Canvas canvas) {
if (drawables != null) {
Drawable drawable = drawables[1]; // top
if (drawable != null) {
final float textY = getY() + drawable.getIntrinsicHeight() + fFontHeight - 5;
canvas.drawText(getText().toString(), 0, textY, mPaint);
canvas.clipRect(drawable.getBounds());
canvas.drawBitmap(getBitmap(drawable), getLeft()+leftMargin, getTop(), mPaint);
drawable.draw(canvas);
canvas.save();
}
}
super.onDraw(canvas);
} private final static Bitmap getBitmap(Drawable drawable){
BitmapDrawable bd = (BitmapDrawable) drawable;
return bd.getBitmap();
}
}
<com.bluetooth.light.widget.DrawableTopLeftTextView
android:id="@+id/signal_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:drawableTop="@drawable/signal_logo"
android:text="@string/signal"
android:textColor="#cccccccc"
android:textSize="24sp" />

效果图

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. zoj3433(贪心+优先队列)

    Gu Jian Qi Tan Time Limit: 2 Seconds      Memory Limit: 65536 KB Gu Jian Qi Tan is a very hot Chines ...

  2. 【转】android 物理按键

    关键词:android   按键  矩阵按键 AD按键  平台信息: 内核:linux2.6/linux3.0 系统:android/android4.0 平台:S5PV310(samsung exy ...

  3. ubuntu 文件readonly的问题: W10: Warning: Changing a readonly file 解决办法

    日前,笔者遇到一个奇怪且让人蛋疼的问题,借用别人的话"大家在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warn ...

  4. java学习之数据库

    第一部分:DB2基础知识 一.复习 jre jdk jvm path classpath 二.数据库基础-DB2 1.访问172.22.602.24/ temp 123 2.安装检查 db2cmd d ...

  5. exchange邮箱的”单点登陆“

    在跟exchange集成登陆时,通常有这样的需求,用户需要点击邮件链接的时候直接打开,不再需要输入用户名密码,实现所谓的单点登陆. 通常有两种方式 1.form认证 登陆原理:用js模拟表单登陆 代码 ...

  6. WebService调用1(.Net)

    1.创建一个最简单的Web Service (1)  新建-项目-ASP.NET空WEB应用程序 (2)添加新项-WEB服务 默认会添加一个HelloWorld方法: using System; us ...

  7. 简单的oracle sql 语句

    创建表空间 create tablespace qnhouse --表空间文件路径 datafile 'E:\qnhost\qnhouse.dbf' --表空间文件大小 size 100M; 创建用户 ...

  8. delphi按钮控件的default属性

    delphi按钮控件的default属性用于设置默认命令按钮,.设置为true时,按[Enter键]相当于用鼠标单击了该按钮 .窗口中如果有多个按钮的default是true的话,就根据tabinde ...

  9. qq电脑管家抽奖页面源代码

    桌面qq电脑管家中个人中心的抽奖页面竟然是HTML写的,光大我HTML.!!! <!DOCTYPE html> <html> <head lang="en&qu ...

  10. java 使用pem密钥进行RSA加解密

    1.使用openssl生成私钥和公钥 openssl下载地址:http://www.openssl.org/source openssl生成私钥命令:  genrsa -out rsa_private ...