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. HDU5518 : John's Fences

    求出平面图的对偶图,那么需要选择一些环,使得这些环可以异或出所有环. 对于两个不同的区域,需要用一个代价最小的环把它们区分开,这对应最小割. 那么求出对偶图的最小割树,所有树边之和就是把所有区域都区分 ...

  2. vb.net播放资源文件中的音乐

    1.在自己的工程里添加一个资源文件. 2.打开添加的资源文件,资源类型选择为音频,点击添加资源把准备好的wav格式音乐文件添加进入资源文件. 3.设置资源属性和文件属性为嵌入 4.代码以及调用方法 P ...

  3. python中 is 和 == 的区别

    is 是比较两个引用是否指向了同一个对象(引用比较). == 是比较两个对象是否相等.

  4. js+css3+HTML5拖动滑块(type="range")改变值

    最近在做一个H5的改版项目,产品和设计给出的效果中有一个拖动滑块可以改变输入值的效果,类似如下图这样: 拿到这样的设计稿后,我有点懵了,自己写一个js?去网上找一个这样的效果?自己写一个可以,只是实现 ...

  5. Hibernate(12)_基于主键的双向1对1

    一.基于主键的双向1对1 1.介绍: 基于主键的映射策略:指一端的主键生成器使用 foreign 策略,表明根据"对方"的主键来生成自己的主键,自己并不能独立生成主键. <p ...

  6. JIRA、Confluence中文官方技术博客

    JIRA.Confluence中文官方技术博客 http://blog.csdn.net/atlassian2013

  7. 简易RPC框架-SPI

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 01、Spar内核架构原理

        附件列表

  9. dlib的编译和安装

    之前写过python dlib依赖的安装,安装过程还算比较复杂,还需要安装boost.Python依赖等,但是如果纯粹的编译C++的dlib库,则要简单得多,基本上不需要其他外部的依赖,这里简单叙述一 ...

  10. windows下Graphviz安装及入门教程

    下载安装配置环境变量 intall 配置环境变量 验证 基本绘图入门 graph digraph 一个复杂的例子 和python交互 发现好的工具,如同发现新大陆.有时,我们会好奇,论文中.各种专业的 ...