zoukankan      html  css  js  c++  java
  • CustomDrawableTextView

    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'
     
  • 相关阅读:
    C——联合体(共同体)总结
    JMX操作ActiveMQ(1)
    使用xml和java代码混合控制UI界面
    Hive Metastore ObjectStore PersistenceManager自动关闭bug解析
    (算法课大报告)大数据的查找与排序
    编程珠玑---读书笔记---使用后缀数组查找最长重复子串
    VMware vSphere服务器虚拟化实验十一高可用性之三Fault Tolerance
    签名应用例子
    fopen()惹的祸
    Bigcommerce: 给已完成购买的客户发送一封产品评论邮件,让客户直接进行产品评论
  • 原文地址:https://www.cnblogs.com/tt_mc/p/8504323.html
Copyright © 2011-2022 走看看