是否有可能扩大drawableleft和放大器; drawableright在TextView中? [英] Is it possible to scale drawableleft & drawableright in textview?

查看:160
本文介绍了是否有可能扩大drawableleft和放大器; drawableright在TextView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表,该项目是一个的TextView ;并采用drawableleft和放大器; drawableright到beutify的的TextView 。 问题是,无论何时在TextView的文字是较大的,drawableleft&安培; drawableleft没有自动规模基础上,的TextView 的高度。

I have list with the item is a TextView; and using drawableleft & drawableright to beutify the TextView. the problem is, whenever the text in textview is larger, drawableleft & drawableleft didn't automatically scale based on the TextView's height.

是否有可能扩大drawableleft与放大器的高度; drawableright在TextView中? (我用的是9片图像)

Is it possible to scale the height of drawableleft & drawableright in textview ? (I was using 9 patch image)

推荐答案

我通过引入ScaleDrawable并重写其.getIntrisicHeight(),因此,它是至少所述的TextView高度解决的等效用例。该TextView.addOnLayoutChangeListener部分,来重新绑定在一个TextView尺寸变化绘制对象要求适用于API11 +

I solved an equivalent usecase by introducing a ScaleDrawable and overriding its .getIntrisicHeight() so that it is at least the TextView height. The TextView.addOnLayoutChangeListener part, required to rebind the Drawable on a TextView size change works with API11+

Drawable underlyingDrawable = 
        new BitmapDrawable(context.getResources(), result);

// Wrap to scale up to the TextView height
final ScaleDrawable scaledLeft = 
        new ScaleDrawable(underlyingDrawable, Gravity.CENTER, 1F, 1F) {
    // Give this drawable a height being at
    // least the TextView height. It will be
    // used by
    // TextView.setCompoundDrawablesWithIntrinsicBounds
    public int getIntrinsicHeight() {
        return Math.max(super.getIntrinsicHeight(), 
                        competitorView.getHeight());
    };
};

// Set explicitly level else the default value
// (0) will prevent .draw to effectively draw
// the underlying Drawable
scaledLeft.setLevel(10000);

// Set the drawable as a component of the
// TextView
competitorView.setCompoundDrawablesWithIntrinsicBounds(
        scaledLeft, null, null, null);

// If the text is changed, we need to
// re-register the Drawable to recompute the
// bounds given the new TextView height
competitorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

    @Override
    public void onLayoutChange(View v, int left, int top, int right, 
            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        competitorView.setCompoundDrawablesWithIntrinsicBounds(scaledLeft, null, null, null);
    }
});

这篇关于是否有可能扩大drawableleft和放大器; drawableright在TextView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆