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

来源:互联网 发布:linux如何切换用户 编辑:程序博客网 时间:2024/05/21 17:15
  1. 转自:http://blog.csdn.net/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. }  

原创粉丝点击