Android - 动态设置TextView的高度 [英] Android - Set Height to TextView Dynamically
本文介绍了Android - 动态设置TextView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨大师,
我有一些与TextView高度相关的关键问题。我想根据运行时设置的内容设置TextView的高度。
这是我的xml代码: -
Hi Gurus,
I have some critical issue related to height of TextView. I want to set the height of the TextView based on the content which is set at runtime.
Here is my xml code:-
< TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/ >
这是设置TextView高度的java代码: -
Here is the java code to set the height of the TextView:-
TextView tv = (TextView) findViewById(R.id.tv);
tv.invalidate();
int height_in_pixels = tv.getLineCount() * tv.getLineHeight();
tv.setHeight(height_in_pixels);
但问题是;它只显示1行,而不显示其余行。
But the problem is; it displays only 1 line, and not the rest of the lines.
推荐答案
Friends,
Use this way :
<pre lang="xml">
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="15dp" <!-- HEIGHT only example -->
android:autoSizeTextType="uniform"/>
这篇关于Android - 动态设置TextView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文