我试图在文本 block 旁边显示一条蓝线,就像这样:
这是我的代码:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/blue_line" />
blue_line 是一个 jpg 文件。一个蓝色矩形。无论 TextView 中的文本如何,它都会以原始大小显示。如何根据文本的高度动态调整其高度?比如文字少的时候变短,文字多的时候变长……
最佳答案
您可以尝试在代码中通过设置图像边界来实现
textView1.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Drawable img = ActivityName.this.getContext().getResources().getDrawable(
R.drawable.blue_line);
img.setBounds(0, 0, img.getIntrinsicWidth() * textView1.getMeasuredHeight() / img.getIntrinsicHeight(), textView1.getMeasuredHeight());
textView1.setCompoundDrawables(img, null, null, null);
textView1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
关于android:在textView中适合DrawableLeft的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22218486/