是否有可能扩大drawableleft和放大器; drawableright在TextView中?有可能、放大器、TextView、drawableleft

2023-09-05 05:21:16 作者:樱花浪漫

我的列表,该项目是一个的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);
    }
});