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

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

在开发过程中我们往往会遇到图片旁边带文字的布局,这种布局有些比较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);
    }

}

相关文章
|
JSON Kubernetes Unix
Kubernetes crictl管理命令详解
Kubernetes crictl管理命令详解
【Bug】Android resource linking failed和error: failed linking references.
【Bug】Android resource linking failed和error: failed linking references.
|
缓存 算法 大数据
倚天710规模化应用 - 性能优化 - 软件预取分析与优化实践
软件预取技术是编程者结合数据结构和算法知识,将访问内存的指令提前插入到程序,以此获得内存访取的最佳性能。然而,为了获取性能收益,预取数据与load加载数据,比依据指令时延调用减小cachemiss的收益更大。
|
机器学习/深度学习 人工智能 开发工具
Clone-voice:开源的声音克隆工具,支持文本转语音或改变声音风格,支持16种语言
Clone-voice是一款开源的声音克隆工具,支持16种语言,能够将文本转换为语音或将一种声音风格转换为另一种。该工具基于深度学习技术,界面友好,操作简单,适用于多种应用场景,如视频制作、语言学习和广告配音等。
2192 9
Clone-voice:开源的声音克隆工具,支持文本转语音或改变声音风格,支持16种语言
|
网络协议 Java
在Java中使用TCP协议搭建一个简单的客户端
如何在Java中使用TCP协议搭建一个简单的客户端
539 2
|
Shell 调度 Docker
在Docker中,如何清理批量后台停止的容器?
在Docker中,如何清理批量后台停止的容器?
|
开发工具 Android开发
Android Studio resource linking failed
Android Studio resource linking failed
412 1
TabLayout app:tabMode和app: tabGravity配合使用效果对比
TabLayout app:tabMode和app: tabGravity配合使用效果对比
|
Java 网络安全
SSL peer shut down incorrectly
SSL peer shut down incorrectly
1387 0
|
Kotlin
找不到 kotlinx.android.synthetic***
找不到 kotlinx.android.synthetic***
588 0