如何快速获得textview字体大小的宽度和高度使用Paint.getTextBounds

1111人阅读
在做项目的时候,很多时候我们都要用到文字和图片一起显示,一般设置TextView的DrawableLeft、DrawableRight、DrawableTop、DrawableBottom就行了。但是有一种情况是当TextView的熟悉是fill_parent或者使用权重的时候并且设置了起Gravity的ceter的时候,Drawable图片是无法一起居中的,为了解决其,我们一般再套一层布局,然后设置TextView的熟悉是wrap_content,但是有时候嵌套过多的布局的时候,有可能发生StackOverFlow,所以必须要优化,下面说一下其中的一个解决方案。先上图
这个解决方案很粗糙,局限性很大,文字不能换行,换行之后就不准了,下面是源码:
package com.example.
import java.lang.ref.WeakR
import android.content.C
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.C
import android.graphics.C
import android.graphics.R
import android.util.AttributeS
import android.view.MotionE
import android.widget.TextV
public class DrawableTextView extends TextView {
private WeakReference&Bitmap& normalR
private WeakReference&Bitmap& pressR
private WeakReference&Bitmap& showR
private int normalColor = Color.WHITE, pressColor = Color.WHITE;
private int textWidth = 0;
private int textHeight = 0;
public DrawableTextView(Context context) {
super(context);
public DrawableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
public DrawableTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
protected void onFinishInflate() {
super.onFinishInflate();
initText();
private void initText() {
text = super.getText().toString();
initVariable();
* 初始化,测量Textview内容的长度,高度
private void initVariable() {
textWidth = (int) (getPaint().measureText(text));
final Rect rect = new Rect();
getPaint().getTextBounds(text, 0, 1, rect);
textHeight = rect.height();
* 设置TextView的内容
* @param text
public void setText(String text) {
this.text =
initVariable();
invalidate();
* 获取TextView内容
public String getText() {
* 设置TextView的Drawable内容,目前仅支持DrawableLeft
* @param normalDrawableId
DrawableLeft的normal状态Id
* @param pressDrawableId
DrawableLeft的press状态的Id(没有press状态,请传-1)
public void setDrawableLeftId(final int normalDrawableId, final int pressDrawableId) {
normalReference = new WeakReference&Bitmap&(BitmapFactory.decodeResource(getResources(), normalDrawableId));
if (pressDrawableId != -1) {
pressReference = new WeakReference&Bitmap&(BitmapFactory.decodeResource(getResources(), pressDrawableId));
showReference = normalR
invalidate();
* 设置TextView的Color
* @param normalColor
TextView normal状态的Color值
* @param pressDrawableId
TextView press状态的Color值(如果没有press状态,请传与normal状态的值)
public void setTextColor(final int normalColor, final int pressColor) {
this.normalColor = normalC
this.pressColor = pressC
getPaint().setColor(normalColor);
initVariable();
protected void onDraw(Canvas canvas) {
if (showReference != null && showReference.get() != null) {
final int bitmapWidth = showReference.get().getWidth();
final int bitmapHeight = showReference.get().getHeight();
final int viewHeight = getHeight();
final int drawablePadding = getCompoundDrawablePadding();
final int start = (getWidth() - (bitmapWidth + drawablePadding + textWidth)) && 1;
canvas.drawBitmap(showReference.get(), start, (viewHeight && 1) - (bitmapHeight && 1), getPaint());
* 注意改方法,第三个参数y,本人也被误导了好久,原来在画文字的时候,y表示文字最后的位置(不是下笔点的起始位置)
* 所以为什么 是TextView高度的一半(中间位置) + 文字高度的一半 = 文字居中
canvas.drawText(text, start + drawablePadding + bitmapWidth, (viewHeight && 1) + (textHeight && 1), getPaint());
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (pressReference != null && pressReference.get() != null) {
showReference = pressR
getPaint().setColor(pressColor);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (normalReference != null && normalReference.get() != null) {
showReference = normalR
getPaint().setColor(normalColor);
invalidate();
return super.onTouchEvent(event);
}xml布局:
&com.example.testandroid.DrawableTextView
android:id=&@+id/my_textview&
android:layout_width=&fill_parent&
android:layout_marginTop=&20dp&
android:background=&@drawable/text_selector&
android:drawablePadding=&8dp&
android:textColor=&@color/standard_orange&
android:layout_height=&wrap_content&
android:padding=&15dp&
android:textSize=&16sp&
android:text=&有Drawable的TextView& /&调用代码:
DrawableTextView drawableTextView = (DrawableTextView) getView().findViewById(R.id.my_textview);
drawableTextView.setDrawableLeftId(R.drawable.bg_btn_delete_normal, R.drawable.bg_btn_delete_pressed);
drawableTextView.setTextColor(getResources().getColor(R.color.standard_orange), getResources().getColor(R.color.standard_white));
drawableTextView.setText(&我在动态修改Text啦&);
其实还有更加方便的方法,下面朋友借鉴某个网友的代码(地址我就不知道了):
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableLeft = drawables[0];
if (drawableLeft != null) {
final float textWidth = getPaint().measureText(getText().toString());
final int drawablePadding = getCompoundDrawablePadding();
final int drawableWidth = drawableLeft.getIntrinsicWidth();
final float bodyWidth = textWidth + drawableWidth + drawableP
canvas.translate((getWidth() - bodyWidth) / 2, 0);
super.onDraw(canvas);
&com.example.testandroid.DrawableTextView
android:id=&@+id/my_textview&
android:layout_width=&fill_parent&
android:layout_marginTop=&20dp&
android:background=&@drawable/text_selector&
android:drawablePadding=&8dp&
android:drawableLeft=&@drawable/clear_edittext_selector&
android:textColor=&@color/text_color_selector&
android:layout_height=&wrap_content&
android:padding=&15dp&
android:textSize=&16sp&
android:text=&有Drawable的TextView& /&
嗯,自己写这个东西,也学到了一些东西,大家有什么更好的方法,大家可以讨论一下。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:87227次
积分:1390
积分:1390
排名:第13947名
原创:46篇
评论:101条
(1)(1)(3)(2)(6)(4)(1)(4)(6)(7)(5)(9)(8)ios 获取光标的位置--动态改变textView的高度的时候用。 - 张明炜 - ITeye技术网站
博客分类:
//设置光标到输入文字的末尾
  NSUInteger length = m_textInput.text.
  m_textInput.selectedRange = NSMakeRange(length,0);
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
[selfaddToolBar];
if ([textView.text isEqualToString:@"请输入..."]) {
textView.text = @"";
if (kScreenHeight == 480) {
[UIViewanimateWithDuration:0.3animations:^{
[self.moScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];
self.moScrollView.bottom -= 55;
CGSize sizeToFit = [textView.text sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:CGSizeMake(232, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型(lineBreakMode)可根据自己的实际情况进行设置
if (sizeToFit.height & 40) {
[self.moScrollViewsetContentOffset:CGPointMake(0, 0) animated:YES];
self.moScrollView.bottom -= (sizeToFit.height-30);
returnYES;
- (void)textViewDidChange:(UITextView *)textView
reservedRange = textView.selectedR
textView.selectedRange = NSMakeRange(textView.text.length, 0);
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
NSLog(@"===%f,%f",cursorPosition.y,cursorPosition.x);
textView.selectedRange = reservedR
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
CGFloat heightNum = textView.contentSize.height;
NSLog(@"heightNum===%f",heightNum);
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
NSLog(@"===%f,%f",cursorPosition.y,cursorPosition.x);
NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text]; //得到输入框的内容
if ([toBeString length] & 30) { //如果输入框内容大于20则弹出警告
textView.text = [toBeString substringToIndex:30];
return NO;
if ([text isEqualToString:@"\n"]) {
heightNum = textView.contentSize.height+15;
heightNum = textView.contentSize.height;
if (cursorPosition.x&5) {
heightNum = heightNum-20;
if (heightNum &60) {
heightNum = heightNum - 30;
if (heightNum & 30) {
[textView setFrame:CGRectMake(textView.left, textView.top, textView.width, heightNum)];
self.fifthView.frame = CGRectMake(self.fifthView.left, self.fifthView.top, self.fifthView.width, heightNum+10);
self.fifthViewImg.height = self.fifthView.height;
self.moScrollView.contentSize = CGSizeMake(320, 467+15+heightNum-30);
[self.moScrollViewsetContentOffset:CGPointMake(0, heightNum-30) animated:YES];
UIImage *image = [UIImageimageNamed:@"儿童培训-修改资料-列表-中.png"];
UIImage *image1 = [image stretchableImageWithLeftCapWidth:310/2topCapHeight:40/2];
self.fifthViewImg.image = image1;
self.chageframView.top = self.fifthView.bottom;
returnYES;
浏览: 239829 次
来自: 开封
你的demo下载了,但运行错误,还要怎么设置?
好的 谢谢 ,收藏
gongjinglin1989 写道大神,能知道下那个java ...
大神,能知道下那个java服务端的实现嘛?数据量多了我,我不知 ...
楼主 总结的 挺全面啊,谢谢。主题 : 如何动态设置textview的高度
级别: 骑士
可可豆: 1480 CB
威望: 1480 点
在线时间: 412(时)
发自: Web Page
来源于&&分类
如何动态设置textview的高度&&&
动态设置textview的高度,怎么设置,因为传过来的内容不固定。
级别: 骑士
可可豆: 1480 CB
威望: 1480 点
在线时间: 412(时)
发自: Web Page
怎么实现sizeToFit?
级别: 侠客
可可豆: 1301 CB
威望: 1301 点
在线时间: 140(时)
发自: Web Page
CGSize _size = [_string sizeWithFont:[UIFont&&boldSystemFontOfSize:14]];int w = _textView.frame.size.int row = (int) _size.width / w + 1;[_textView setFrame:CGRectMake(0,0, w, row * _size.height + 15)];string为你的串, 字体你可以换。
关注本帖(如果有新回复会站内信通知您)
3*3+1 正确答案:10
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
阅读(1156)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'TextView超出屏幕宽度字符的处理',
blogAbstract:'在TextView中显示文本时,如果文本的内容过长,会换行导致布局与预期有所差异。在mars-chen的教程里看到,一般采用的方法是将超出屏幕宽度的字符用省略号表示。
只需要在Layout中添加两个属性:
&TextView android:id=\"@+id/textid\"
&&&&&&&&&&android:layout_width=\"fill_parent\"
&&&&&&&&&&android:layout_height=\"wrap_content\"
&&&&&&&&&&android:ellipsize=\"end\"',
blogTag:'android,textview',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:7,
publishTime:5,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}如何快速获得TextView的宽度和高度使用Paint.getTextBounds_百度知道
如何快速获得TextView的宽度和高度使用Paint.getTextBounds
提问者采纳
如果还有问题,可以继续追问. oncreate方法
先执行这个
获得xml的资源
TextView tv = (TextView) findViewById(R;&#47.TextViewId如果没有设置width&#47,感谢您好.setContentView(R.setWidth().xml).TextViewId)
其他类似问题
textview的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 textview属性 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信