TextView中DrawableXXX图片无法设置大小的解决方案

在开发过程中我们往往会遇到图片旁边带文字的布局,这种布局有些比较Low的开发会直接用一个ImageView和TextView,有经验的会给TextView设置DrawableLeft、DrawableRight等等属性,一个View搞定,但是这个属性设置图片是无法控制大小的,在xml里面,当然在Java代码里是可以设置的。

        TextView textView = new TextView(mContext);
        Drawable drawable = getResources().getDrawable(R.drawable.icon_friend);
        // 设置图片的大小
        drawable.setBounds(0, 0, 20, 20);
        // 设置图片的位置,左、上、右、下
        textView.setCompoundDrawables(null, null, drawable, null);

当然,我们还可以用自定义View来实现这个效果,代码也是非常的简单

<!-- 图片文字自定义属性 -->
    <declare-styleable name="DrawableTextView">
        <attr name="drawableLeft" format="reference"/>
        <attr name="drawableBottom" format="reference"/>
        <attr name="drawableRight" format="reference"/>
        <attr name="drawableTop" format="reference"/>
        <attr name="drawableWidth" format="dimension"/>
        <attr name="drawableHeight" format="dimension"/>
    </declare-styleable>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import com.rzj.zhongshi.R;

public class DrawableTextView extends android.support.v7.widget.AppCompatTextView {
    private Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
     drawableBottom = null;
    private int drawableWidth, drawableHeight;
    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
        int count = typedArray.getIndexCount();
        for(int i = 0; i < count; i++){
            int attr = typedArray.getIndex(i);
            switch (attr){
                case R.styleable.DrawableTextView_drawableRight:
                    drawableRight = typedArray.getDrawable(attr);
                    break;
                case R.styleable.DrawableTextView_drawableLeft:
                    drawableLeft = typedArray.getDrawable(attr);
                    break;
                case R.styleable.DrawableTextView_drawableTop:
                    drawableTop = typedArray.getDrawable(attr);
                    break;
                case R.styleable.DrawableTextView_drawableBottom:
                    drawableBottom = typedArray.getDrawable(attr);
                    break;
                case R.styleable.DrawableTextView_drawableWidth:
                    drawableWidth = typedArray.getDimensionPixelSize(attr, 0);
                    break;
                case R.styleable.DrawableTextView_drawableHeight:
                    drawableHeight = typedArray.getDimensionPixelSize(attr,0);
                    break;
            }
        }
        if(null != drawableLeft){
            drawableLeft.setBounds(0,0, drawableWidth, drawableHeight);
        }
        if(null != drawableRight){
            drawableRight.setBounds(0,0, drawableWidth, drawableHeight);
        }
        if(null != drawableTop){
            drawableTop.setBounds(0,0, drawableWidth, drawableHeight);
        }
        if(null != drawableBottom){
            drawableBottom.setBounds(0,0, drawableWidth, drawableHeight);
        }
        setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值