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);
    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,671评论 25 709
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,130评论 3 119
  • 感赏本宝宝越来越能干了,既进得了厨房又会穿衣打扮,出得厅堂下得了厨房,既能认真工作又会享受生活,还养了一个聪明懂事...
    丽丽丫丫阅读 290评论 0 0
  • 英文版 You say that you love rain, but you open your umbrell...
    傲子熙阅读 323评论 0 0
  • 一 首先必须要有一个核心 让大家山呼万岁 还必须是倒金字塔结构 不然既没有登顶的疯狂 也没有坐穿井底的清寂 所有的...
    小知识阶层阅读 195评论 2 3

友情链接更多精彩内容