自定义控件使用DrawableLeft/Right图片和文字同时居中

自定义居中TextView
本文介绍了一种自定义TextView的方法,使drawable与文本在水平方向上居中显示。包括两个自定义控件示例:一个用于drawableLeft与文本居中显示;另一个用于drawableRight与文本居中显示。
  1. 转自:http://blog.youkuaiyun.com/eileenching/article/details/48393531
  2. /** 
  3.  * 自定义控件,使用drawableLeft与text水平居中显示 
  4.  */  
  5. public class DrawableLeftCenterTextView extends TextView {  
  6.   
  7.     public DrawableLeftCenterTextView(Context context) {  
  8.         super(context);  
  9.     }  
  10.   
  11.     public DrawableLeftCenterTextView(Context context, AttributeSet attrs) {  
  12.         super(context, attrs);  
  13.     }  
  14.       
  15.     public DrawableLeftCenterTextView(Context context, AttributeSet attrs,  
  16.             int defStyle) {  
  17.         super(context, attrs, defStyle);  
  18.     }  
  19.       
  20.     @Override  
  21.     protected void onDraw(Canvas canvas) {  
  22.         Drawable[] drawables = getCompoundDrawables();  
  23.         if(drawables != null){  
  24.             Drawable drawableLeft = drawables[0];  
  25.             if(drawableLeft != null){  
  26.                 float textWidth = getPaint().measureText(getText().toString());  
  27.                 int drawablePadding = getCompoundDrawablePadding();  
  28.                 int drawableWidth = 0;  
  29.                 drawableWidth = drawableLeft.getIntrinsicWidth();  
  30.                 float bodyWidth = textWidth + drawableWidth + drawablePadding;  
  31.                 canvas.translate((getWidth() - bodyWidth) / 20);  
  32.             }  
  33.         }  
  34.         super.onDraw(canvas);  
  35.     }  
  36.       
  37.   
  38. }  






代码二:

[java]  view plain  copy
  1. /** 
  2.  * 自定义控件,使用drawableRight与text水平居中显示 
  3.  */  
  4. public class DrawableRightCenterTextView extends TextView {  
  5.   
  6.     public DrawableRightCenterTextView(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public DrawableRightCenterTextView(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.     }  
  13.       
  14.     public DrawableRightCenterTextView(Context context, AttributeSet attrs,  
  15.             int defStyle) {  
  16.         super(context, attrs, defStyle);  
  17.     }  
  18.       
  19.     @Override  
  20.     protected void onDraw(Canvas canvas) {  
  21.         Drawable[] drawables = getCompoundDrawables();//left,top,right,bottom  
  22.         if(drawables != null){  
  23.             Drawable drawableRight = drawables[2];  
  24.             if(drawableRight != null){  
  25.                 float textWidth = getPaint().measureText(getText().toString());  
  26.                 int drawablePadding = getCompoundDrawablePadding();  
  27.                 int drawableWidth = 0;  
  28.                 drawableWidth = drawableRight.getIntrinsicWidth();  
  29.                 float bodyWidth = textWidth + drawableWidth + drawablePadding;  
  30.                 setPadding(00, (int)(getWidth() - bodyWidth), 0);  
  31.                 canvas.translate((getWidth() - bodyWidth) / 20);  
  32.             }  
  33.         }  
  34.         super.onDraw(canvas);  
  35.     }  
  36.       
  37.   
  38. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值