机器人:DrawableLeft在一个TextView合适的高度 [英] android: fit height of DrawableLeft in a textView

查看:222
本文介绍了机器人:DrawableLeft在一个TextView合适的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个蓝色的线旁的文本块,pretty的多是这样的:

I am trying to display a blue line next to a block of text, pretty much like this:

下面是我的code:

<TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/blue_line" />

blue_line是一个JPG文件。一个蓝色的矩形。它在原来的大小显示,无论在TextView中的文字。我怎么能动态地调整其高度根据文本的高度?喜欢把它缩短即使世界上的文字少量和更长的时候有更多的文字....

blue_line is a jpg file. a blue rectangle. it displays in its original size regardless of the text in the textview. how can i adjust its height dynamically according to the height of the text? like make it shorter when theres little amount of text and longer when there's more text....

推荐答案

您可以尝试通过设置边界,该图像

You can try doing it in code by setting bounds for the image

textView1.getViewTreeObserver()
    .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Drawable img = ActivityName.this.getContext().getResources().getDrawable(
            R.drawable.blue_line);
            img.setBounds(0, 0, 20, textView1.getMeasuredHeight());
            textView1.setCompoundDrawables(img, null, null, null);
            textView1.removeOnLayoutChangeListener(this);
        }
    });

这篇关于机器人:DrawableLeft在一个TextView合适的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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