Android 自己定义 TextView drawableTop 图标与文字左对齐(效果图)
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 图标与文字左对齐(效果图)的更多相关文章
- day3-3种实现小图标与文字水平对齐的方式
效果图: 1.使用小图标作为背景图实现 html: <div class="test"> <ul> <li class="method1&q ...
- css实现一行文字居中,多行文字左对齐
问题及场景: 当内容能一行显示在盒子内时,文字居中对齐. 当内容过多换行后显示在盒子内时,文字左对齐. 其实这种视觉上的需求还是蛮常见的.比如用于弹出提示框,当提示内容比较少时,内容居中显示在弹出框, ...
- android Editview中加小图标或者文字实现
关于这个问题,如果只是加小图标的话,已经提供了很好的支持,drawableLeft属性就可以设置左边的小图标,类推,右边也可以 不过如果你要加的是文字,我找了下,没有相应的属性,我们只能通过转换思路去 ...
- android中给TextView或者Button的文字添加阴影效果
1在代码中添加文字阴影 TextView 有一个方法 /** * Gives the text a shadow of the specified radius and color, the ...
- android 自己定义TextView"会发脾气的TextView"
转载请注明出处王亟亟的大牛路 Git上看到的一个自己定义控件就搞来研究研究.蛮可爱的. 项目结构: 执行效果:非常Q谈.谈的图片什么都 都能够换哦 自己定义View: public class Jel ...
- Android 自己定义TextView 实现文本间距
转载请标明出处: http://blog.csdn.net/u011974987/article/details/50845269: Android系统中TextView默认显示中文时会比較紧凑.不是 ...
- android自己定义TextView
Android控件中的TextView控件仅仅有一个输入框.可是为了用于的操作方便我们应该实现一些功能: 1. 能够直接将内容删除的功能button 2. 可以记录用户曾经输入的数据,同一时候可以将数 ...
- html 图标和文字一行对齐
原图: 效果图: 备注:vertical-align:middle <div> <p class="time tl-size12" style="pad ...
- UIButton 按钮文字左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; btn.titleEdgeInsets = UIEd ...
随机推荐
- 剑指offer-面试题17.合并两个排序的链表
题目:输入两个递增的排序的链表,合并这两个链表并使新链表中的节点仍然是 按照递增排序的.例如链表1链表2合并为链表3. List1:->->-> List2:->->-& ...
- WebBot - Build Web Apps as Easily as Native Ones - Home
Python-based Template Packages Python-based Template Packages WebBot - Build Web Apps as Easily as N ...
- JavaWeb——文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- java File的getLastModified在不同操作系统以下存在差异
java对文件读取改动时间(getLastModified())在不同的操作系统下存在差异 //1.在windows下,返回值是毫秒级别,不存在问题 //2.在Linux下,返回的值是毫秒值,可是会 ...
- LinearGradient线性渲染
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...
- ios应用view之间数据传递的方式
对于不同的viewcontroller之间数据的共享和处理 采用代理的方式,子viewcontroller设计代理协议,并定义协议接口,父viewcontroller实现协议接口,实现子视图控制器退出 ...
- Linux以及Android开发中的小技巧和长繁命令记录收集
不断更新收集中.... 201407161654 ssh以nx_guest的身份登录到172.24.221.137,然后在172.24.221.137与172.24.61.252的8080port建立 ...
- php错误日志级别
; E_ALL 所有错误和警告(除E_STRICT外) ; E_ERROR 致命的错误.脚本的执行被暂停. ; E_RECOVERABLE_ERROR ...
- WIFI网络访问(一)
一,WIFI 网卡有哪些状态? WIFI 总共有以下五个状态,实际就是一些整形常量: 1. WIFI_STATE_DISABLED : WIFI 不能使用,其值是: 1 . 2. WIFI_S ...
- dojo.byId、dojo.query、dojo.attr
概述: dojo.byId(/*string*/id或/*DomNode*/node) 1.传入DOMNode返回传入的domNode; 2.传入id返回id为当前值的domNode dojo.que ...