Android编程中TextView宽度过大导致Drawable无法居中问题解决方法

本文实例讲述了android编程中textview宽度过大导致drawable无法居中问题解决方法。分享给大家供大家参考,具体如下:

在做项目的时候,很多时候我们都要用到文字和图片一起显示,一般设置textview的drawableleft、drawableright、drawabletop、drawablebottom就行了。但是有一种情况是当textview的熟悉是fill_parent或者使用权重的时候并且设置了起gravity的ceter的时候,drawable图片是无法一起居中的,为了解决其,我们一般再套一层布局,然后设置textview的熟悉是wrap_content,但是有时候嵌套过多的布局的时候,有可能发生stackoverflow,所以必须要优化,下面说一下其中的一个解决方案。先上图

这个解决方案很粗糙,局限性很大,文字不能换行,换行之后就不准了,下面是源码:

package com.example.testandroid; 
import java.lang.ref.weakreference; 
import android.content.context; 
import android.graphics.bitmap; 
import android.graphics.bitmapfactory; 
import android.graphics.canvas; 
import android.graphics.color; 
import android.graphics.rect; 
import android.util.attributeset; 
import android.view.motionevent; 
import android.widget.textview; 
public class drawabletextview extends textview { 
 private weakreference bitmap normalreference; 
 private weakreference bitmap pressreference; 
 private weakreference bitmap showreference; 
 private int normalcolor = color.white, presscolor = color.white; 
 private string text; 
 private int textwidth = 0; 
 private int textheight = 0; 
 public drawabletextview { 
 super; 
 public drawabletextview { 
 super; 
 public drawabletextview { 
 super; 
 @override 
 protected void onfinishinflate { 
 super.onfinishinflate; 
 inittext; 
 private void inittext { 
 text = super.gettext.tostring; 
 initvariable; 
 /** 
 * 初始化,测量textview内容的长度,高度 
 private void initvariable { 
 textwidth =  .measuretext); 
 final rect rect = new rect; 
 getpaint.gettextbounds; 
 textheight = rect.height; 
 /** 
 * 设置textview的内容 
 * @param text 
 public void settext { 
 this.text = text; 
 initvariable; 
 invalidate; 
 /** 
 * 获取textview内容 
 public string gettext { 
 return text; 
 /** 
 * 设置textview的drawable内容,目前仅支持drawableleft 
 * @param normaldrawableid 
 * drawableleft的normal状态id 
 * @param pressdrawableid 
 * drawableleft的press状态的id 
 public void setdrawableleftid { 
 normalreference = new weakreference bitmap , normaldrawableid)); 
 if  { 
 pressreference = new weakreference bitmap , pressdrawableid)); 
 showreference = normalreference; 
 invalidate; 
 /** 
 * 设置textview的color 
 * @param normalcolor 
 * textview normal状态的color值 
 * @param pressdrawableid 
 * textview press状态的color值 
 public void settextcolor { 
 this.normalcolor = normalcolor; 
 this.presscolor = presscolor; 
 getpaint.setcolor; 
 initvariable; 
 @override 
 protected void ondraw { 
 if  != null) { 
 final int bitmapwidth = showreference.get.getwidth; 
 final int bitmapheight = showreference.get.getheight; 
 final int viewheight = getheight; 
 final int drawablepadding = getcompounddrawablepadding; 
 final int start =  - ) 1; 
 canvas.drawbitmap, start,  - , getpaint); 
 /** 
 * 注意改方法,第三个参数y,本人也被误导了好久,原来在画文字的时候,y表示文字最后的位置 
 * 所以为什么 是textview高度的一半 + 文字高度的一半 = 文字居中 
 canvas.drawtext + , getpaint); 
 @override 
 public boolean ontouchevent { 
 if  == motionevent.action_down) { 
 if  != null) { 
 showreference = pressreference; 
 getpaint.setcolor; 
 } else if  == motionevent.action_up) { 
 if  != null) { 
 showreference = normalreference; 
 getpaint.setcolor; 
 invalidate; 
 return super.ontouchevent; 

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 =  getview.findviewbyid;
drawabletextview.setdrawableleftid;
drawabletextview.settextcolor.getcolor, getresources.getcolor);
drawabletextview.settext;

其实还有更加方便的方法,下面朋友借鉴某个网友的代码:

@override 
protected void ondraw { 
 drawable[] drawables = getcompounddrawables; 
 if  { 
 drawable drawableleft = drawables[0]; 
 if  { 
 final float textwidth = getpaint.measuretext.tostring); 
 final int drawablepadding = getcompounddrawablepadding; 
 final int drawablewidth = drawableleft.getintrinsicwidth; 
 final float bodywidth = textwidth + drawablewidth + drawablepadding; 
 canvas.translate - bodywidth) / 2, 0); 
 super.ondraw; 

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: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" / 

嗯,自己写这个东西,也学到了一些东西,大家有什么更好的方法,大家可以讨论一下。

希望本文所述对大家android程序设计有所帮助。

Copyright © 2018 百家乐试玩百家乐试玩-百家乐游戏 All Rights Reserved