public class CustomDrawableTextView extends TextView{

    //image width、height
private int imageWidth;
private int imageHeight; private Drawable leftImage;
private Drawable topImage;
private Drawable rightImage;
private Drawable bottomImage; public CustomDrawableTextView(Context context) {
this(context, null);
}
public CustomDrawableTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomDrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomDrawableTextView,0,0);
int countNum = ta.getIndexCount();
for (int i = 0; i < countNum; i++) { int attr = ta.getIndex(i);
if (attr == R.styleable.CustomDrawableTextView_leftImage) {
leftImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_topImage) {
topImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_rightImage) {
rightImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_bottomImage) {
bottomImage = ta.getDrawable(attr);
} else if (attr == R.styleable.CustomDrawableTextView_imageWidth) {
imageWidth = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
} else if (attr == R.styleable.CustomDrawableTextView_imageHeight) {
imageHeight = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
}
} ta.recycle();
init();
} /**
* init views
*/
private void init() {
setCompoundDrawablesWithIntrinsicBounds(leftImage,topImage,rightImage,bottomImage);
} @Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { if(left != null) {
left.setBounds(0,0,imageWidth,imageHeight);
} if(top != null) {
top.setBounds(0,0,imageWidth,imageHeight);
} if(right != null) {
right.setBounds(0,0,imageWidth,imageHeight);
} if(bottom != null) {
bottom.setBounds(0,0,imageWidth,imageHeight);
} setCompoundDrawables(left,top,right,bottom);
}
}
<declare-styleable name="CustomDrawableTextView" >
<attr name="leftImage" format="reference" />
<attr name="topImage" format="reference" />
<attr name="rightImage" format="reference" />
<attr name="bottomImage" format="reference" />
<attr name="imageWidth" format="dimension" />
<attr name="imageHeight" format="dimension" />
</declare-styleable>
app:imageHeight="50dp" //图片高度

app:imageWidth="50dp" //图片宽度

app:leftImage="@drawable/ic_qq" //左部图片

app:topImage="@drawable/ic_qq" //顶部图片

app:rightImage="@drawable/ic_qq" //右部图片

app:bottomImage="@drawable/ic_qq" //底部图片

compile 'com.github.czy1121:roundbutton:1.0.0'
 compile 'com.song:CustomDrawableTextView:1.0.0'
 

CustomDrawableTextView的更多相关文章

随机推荐

  1. 关于在ViewPager的子页面中不能跳转的问题

    关于在ViewPager的子页面中不能跳转的问题 什么是ViewPager? 相信很多人使用过微信,其实微信的四个子页面:微信.通讯录.发现.我以及下面的引导空间就是ViewPager. 出现的问题: ...

  2. linux 常用命令解压压缩

    tar -zxvf filename.tar.gz // 解包 tar -xvf filename.tar // 解包 tar -zcvf filename.tar.gz target // 制作ta ...

  3. consul 文档

    consul 服务发现 服务发现,用docker的时候可以使用,并且可以实现负载均衡. 因业务需要,所以留一下自己搜到比较好的资料吧 英文:https://www.consul.io/intro/ge ...

  4. 【分块】教主的魔法 @洛谷P2801/upcexam3138

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列, ...

  5. Kworkerd恶意挖矿分析

    转自:https://www.360zhijia.com/anquan/417114.html 0x01 快速特征排查 TOP显示CPU占用高,但是没有高占用的进程 存在与未知服务器13531端口建立 ...

  6. Unity 5.4版本 Application.systemLanguage 失效

    最近在上线双语版本(一个包支持中文.英文二种语言)时,遇到一个坑点 if (ToolUtils.isAndroid()) { if (Application.systemLanguage == Sys ...

  7. golang使用chan注意事项

    背景 最近老代码中遇到的一个问题,表现为: goroutine数量在高峰期上涨,上涨后平峰期将不下来.也就是goroutine泄露 使用pprof看,进程堵塞在chan chan的使用经验 在使用ch ...

  8. 9款国内外垂直领域的在线作图工具:那些可以替代Visio的应用!【转】

    http://www.csdn.net/article/2015-02-12/2823939 摘要:现在越来越多的创业公司都希望提升办公的效率,今天介绍的几款也能提升办公效率,不过它们都属于垂直领域的 ...

  9. redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error(转)

    redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error   windows下安装Redis第一次启动报 ...

  10. jQuery CSS 操作 - offset() 方法

    今天在一个页面需要知道jquery版本号,来决定使用什么样的方法,有以下方式可以获取到 $.fn.jquery $.prototype.jquery 这两种方式都可以获取到jquery的版本号 --- ...