给自己记录点笔记顺便也给用的着的朋友给点参考
设置Textview的Drawableleft等图片的间距的问题
以Textview举例想得到如下图的Textview效果
publicclassMyTextextendsTextView{
publicMyText(Contextcontext){
super(context);
}
publicMyText(Contextcontext,AttributeSetattrs){
super(context,attrs);
}
@Override
protectedvoidonDraw(Canvascanvas){
//得到Drawable集合分别对应左上右下
Drawable[]drawables=getCompoundDrawables();
if(drawables!=null){
//获取右边图片
DrawabledrawableRight=drawables[2];
if(drawableRight!=null){
//获取文字占用长宽
inttextWidth=(int)getPaint().measureText(getText().toString());
inttextHeight=(int)getPaint().getTextSize();
//获取图片实际长宽
intdrawableWidth=drawableRight.getIntrinsicWidth();
intdrawableHeight=drawableRight.getIntrinsicHeight();
//setBounds修改Drawable在View所占的位置和大小,对应参数同样的左上右下()
drawableRight.setBounds(((textWidth-getWidth())/2),(textHeight-drawableHeight)/2,((textWidth-getWidth())/2)+drawableWidth,(textHeight+drawableHeight)/2);
}
}
super.onDraw(canvas);
}
}
分 -->
|