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. C++程序设计方法3:虚函数

    向上映射与向下映射 派生类对象转换成基类对象,称为向上映射. 而基类对象转换成派生类对象,称为向下映射: 向上映射可由编译器自动完成,是一种隐式的自动类型转化: 凡是接受基类对象的地方(如函数参数)都 ...

  2. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习8

    #include <iostream>using namespace std;const int Size=20;struct Pizza{ char company[Size]; dou ...

  3. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习6

    #include <iostream>using namespace std;struct CandyBar{ char kind[20]; float weight; int calor ...

  4. SourceTree安装教程和破解教程

    SourceTree破解版是一款非常实用的编程工具,这是一款专业的Git和Hg客户端,界面简洁,操作简单易上手,是开发者的必备工具,欢迎大家来绿色资源网下载体验!SourceTree是一款免费的Git ...

  5. Ajax发送请求,并接受字符串

    前台: $.ajax({ type: 'POST', url: url, dataType: "json", success : function(data){ alert(&qu ...

  6. [原创]Burp Suite web应用程序渗透测试神器

    [原创]Burp Suite web应用程序渗透测试神器 一 Burp Suite介绍 Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描 ...

  7. ASP.NET WebApi 基于分布式Session方式实现Token签名认证

    一.课程介绍 明人不说暗话,跟着阿笨一起学玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NETWebSer ...

  8. 821. 字符的最短距离 c++实现方法

    1.题目描述 给定一个字符串 S 和一个字符 C.返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组. 示例 1: 输入: S = "loveleetcode&q ...

  9. SAP传输请求自动发布

        最近公司服务器做迁移,原R3 PRE需要迁到另外的地方,迁移后一段时间内,需要两套PRE环境同时运行,过一段时间后才将传输路线切换到新的PRE.在切换前,要求新PRE环境也要正常同步发布请求, ...

  10. NLP 第7章 文本向量化