让TextView的drawableLeft与文本一起居中显示
TextView的drawableLeft、drawableRight和drawableTop是一个常用、好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享。
声明
欢迎转载,请注明出处!
博客园:http://www.cnblogs.com/
农民伯伯: http://www.cnblogs.com/over140/
/**
* drawableLeft与文本一起居中显示
*/
public class DrawableCenterTextView extends TextView { public DrawableCenterTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
} public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public DrawableCenterTextView(Context context) {
super(context);
} @Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableLeft = drawables[0];
if (drawableLeft != null) {
float textWidth = getPaint().measureText(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableWidth = 0;
drawableWidth = drawableLeft.getIntrinsicWidth();
float bodyWidth = textWidth + drawableWidth + drawablePadding;
canvas.translate((getWidth() - bodyWidth) / 2, 0);
}
}
super.onDraw(canvas);
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:drawableLeft="@drawable/pay_weixin"
android:drawablePadding="10dp"
android:textColor="@color/code08"
android:textSize="16sp"
android:text="微信支付" />
让TextView的drawableLeft与文本一起居中显示的更多相关文章
- 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
前言 TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢 ...
- 重写TextView,实现圆形背景,文本居中显示
最近,在做考试试题排版,产品提出题号希望显示成圆形背景,序号文本居中显示. (有点问题:文本没有绝对居中,暂时没做处理.) 为此,我采取的方式是重写TextView的onDraw方法,绘制一个圆形背景 ...
- 使用代码为textview设置drawableLeft
xml中的textView中设置android:drawableLeft: <TextView android:id="@+id/bookTitle" android:lay ...
- line-height让文本在块级元素中居中显示总结
一.总结: line-height不仅可以用在段落文本中控制行与行之间的间距,还可以用来控制文本在li这种块级元素中的位置. 文本行间距的大小由字体的大小决定,行间距的大小的设置方法有5种方式:
- Ie浏览器TextBox文本未居中
Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中 vertical-align:middle
- Lodop文本项相对于文本框居中 两端对齐
Lodop中ADD_PRINT_TEXT默认内容是相对于文本框居左的,如果想要设置相对于文本框居中,可用如下语句.还有一种是两端对齐,可以让内容的两端阿和文本框的最左和最右端对齐,文本项内容布满文本框 ...
- Android如何在一个TextView中实现多种文本风格?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解Android中,如何在一个 ...
- 【html】合并单元格,并居中显示文本
现状: 想要实现的效果: 代码实现: <tr> <td colspan=" align="center">用例失败为0,无测试详情</td ...
- extjs中grid中行内文本或图片居中显示
我是看了网上写的方法调试自己的代码来实现的,实现的方式是当加载store数据时改变grid的行样式,源码如下: html代码: <div id="weatherP_grid-body& ...
随机推荐
- 10-hibernate单表操作-组件属性
组件属性: 实体类中某个属性属于用户自定义的类的对象,比如在实体类中某个属性是自定义类的对象: 这个Address是一个用户自定义类. 该自定义类Address定义如下: //地址类 public c ...
- Drupal的system_list()函数解析
system_list()函数的目的是根据传入的资源类型,返回一个数组列表: function system_list($type) { ... ... } 参数$type支持下面三种类型: boot ...
- Linux 下Tomcat的启动、关闭、杀死进程
Linux下Tomcat的启动.关闭.杀死进程 打开终端 cd /java/tomcat #执行 bin/startup.sh #启动tomcat bin/shutdown.sh #停止tomcat ...
- Asp.Net Mvc+Localdb数据库项目在IIS部署的配置
1.将数据库文件放到App_Data文件夹下 2.Web.config连接字符串配置 <add name="TestEntities" connectionString=&q ...
- C# 使用TASK处理多任务同时处理
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SQL 将两个结构相同的表合并到成一个表
select * into 新表名 from (select * from T1 union all select * from T2) 这个语句可以实现将合并的数据追加到一个新表中. 不合并重复数据 ...
- CentOS下安装man手册
1.命令: yum install man 后发现,有的函数仍然没有.经过一番查找,原来安装的不完全,还要执行下面命令: yum install man-pages 2.总结:cengos下安装man ...
- atitit. web组件化原理与设计
atitit. web组件化原理与设计 1. Web Components提供了一种组件化的推荐方式,具体来说,就是:1 2. 组件化的本质目的并不一定是要为了可复用,而是提升可维护性. 不具有复用 ...
- c语言循环单链表
/************************************************************************* > File Name: singleLin ...
- zooKeeper_《ZooKeeper官方指南》一致性保障
转 http://ifeve.com/zookeeper-consistency-guarantees/ 本文翻译自<ZooKeeper官方指南>,译者:追云,校对:追云 一致性保障 Zo ...