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

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

概述本文实例讲述了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(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);  }  @OverrIDe  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,1,rect);   textHeight = rect.height();  }  /**   * 设置TextVIEw的内容   * @param text   */  public voID setText(String text) {   this.text = text;   initvariable();   invalIDate();  }  /**   * 获取TextVIEw内容   */  public String getText() {   return text;  }  /**   * 设置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 = normalReference;   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 = normalcolor;   this.presscolor = presscolor;   getPaint().setcolor(normalcolor);   initvariable();  }  @OverrIDe  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());   }  }  @OverrIDe  public boolean ontouchEvent(MotionEvent event) {   if (event.getAction() == MotionEvent.ACTION_DOWN) {    if (pressReference != null && pressReference.get() != null) {     showReference = pressReference;    }    getPaint().setcolor(presscolor);   } else if (event.getAction() == MotionEvent.ACTION_UP) {    if (normalReference != null && normalReference.get() != null) {     showReference = normalReference;    }    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啦");

其实还有更加方便的方法,下面朋友借鉴某个网友的代码(地址我就不知道了):

@OverrIDe 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 + drawablepadding;    canvas.translate((getWIDth() - bodyWIDth) / 2,0);   }  }  super.onDraw(canvas); }

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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程中TextView宽度过大导致Drawable无法居中问题解决方法全部内容,希望文章能够帮你解决Android编程中TextView宽度过大导致Drawable无法居中问题解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1142227.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存