Textview、Button等设置drawableleft的间隔大小的方法

[复制链接]
查看11 | 回复10 | 2021-1-27 06:36:13 | 显示全部楼层 |阅读模式
给自己记录点笔记顺便也给用的着的朋友给点参考

设置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);
}
}
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层



回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
你调节textview或button的width就行了啊
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用2楼Smart_YC的回复:你调节textview或button的width就行了啊
调节之后整个控件大小都改变了点击范围会缩小很多啊这个是在不能改变大小的前提下而且改变padding的话是可以缩进间距但是如果只想放在右下角呢?
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
话说直接xml里面不可以设置么
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
TextView自带属性drawablePadding
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用4楼qq840727854的回复:话说直接xml里面不可以设置么
引用5楼L_tracker的回复:TextView自带属性drawablePadding
图片要在--->右--->下不是单纯靠"右"
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用6楼qq_35532429的回复:Quote: 引用4楼qq840727854的回复:
话说直接xml里面不可以设置么

引用5楼L_tracker的回复:TextView自带属性drawablePadding
图片要在--->右--->下不是单纯靠"右"

做一张.9,上方是空白,下面是那个黑三角形,不就可以吗?
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
如果TextView自带属性drawablePadding不能达到需求为什么不去继承Linerlayout(MyTextextendsLinerlayout)在Linerlayout里面写一个TextView+ImageView

回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用8楼BaiampDanWang的回复:如果TextView自带属性drawablePadding不能达到需求为什么不去继承Linerlayout(MyTextextendsLinerlayout)在Linerlayout里面写一个TextView+ImageView
楼主这方法可取啊,TextView本来就具备了这个方法,稍微改造一下就好,你为啥要去多此一举继承LinearLayout呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行