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. canvas arcTo()用法详解 – CodePlayer

    canvas arcTo()用法详解 – CodePlayer canvas arcTo()用法详解

  2. 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台

    ---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...

  3. PHP MySQL Delete From 之 Delete

    删除数据库中的数据 DELETE FROM 语句用于从数据库表中删除记录. 语法 DELETE FROM table_name WHERE column_name = some_value 注释:SQ ...

  4. Linux如何实现开机启动程序详解

    我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. 加载内核LILO 启动之 ...

  5. ASP.NET中连接数据库的各种方法

    ASP.NET中连接数据库的各种方法 连接SQL数据库的方法:(一).在Web.Config中创建连接字符串:1.<add name="ConnectionString" c ...

  6. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

  7. js库开发

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  8. JavaScript脚本语言的正则校验法

    正则校验法有很多种类型,有些可能会比较复杂难记,我这里罗列了大家常用的几种方法,方便查询. //校验是否全由数字组成 function isShuZi(s) { var patrn=/^[0-9]{1 ...

  9. HDU 5787 K-wolf Number

    题意:l-r之间有多少个数,其连续k位不存在相同的数字 分析:数位dp,从高位开始向低位进行枚举.因为连续k个数字不相同,在枚举一个数字的时候, 要知道前k-1位的内容,这可以用一个4维的数组表示,再 ...

  10. Java——单例设计模式

    设计模式:解决某一类问题最行之有效的方法.Java中23种设计模式:单例设计模式:解决一个类在内存中只存在一个对象. 想要保证对象唯一.1,为了避免其他程序过多建立该类对象.先禁止其他程序建立该类对象 ...