如何动态地设置TextView的高度机器人 [英] How to dynamically set textview height android
问题描述
在我的应用程序需要动态文本设置为我TextView的,所以我希望它得到动态调整大小。我已经设置:
In my application I need to set dynamic text to my textview so I want it to get resized dynamically. I have set:
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:text="Frontview"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#0099CC"
android:singleLine="false"
android:maxLines="4"
android:ellipsize="marquee"
/>
我的TextView的高度不超出2线和文字得到削减。 任何人都可以请帮助?
My textview height is not going beyond 2 lines and the text is getting cut. Can anybody please help?
感谢名单提前。
推荐答案
正如康斯坦丁说,这code可能会被你超过4行之后,除非你删除安卓忽略
As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"
这是你将如何设置code的高度:
This is how you would set the height in code:
TextView tv = (TextView) findViewById(R.id.TextView02);
int height_in_pixels = tv.getLineCount() * tv.getLineHeight(); //approx height text
tv.setHeight(height_in_pixels);
如果你想使用浸台,这使得你的应用程序扩展到多个屏幕尺寸,你会通过返回的值getResources()乘以像素数getDisplayMetrics()密度;
If you want to use dip units, which allows your app to scale across multiple screen sizes, you would multiply the pixel count by the value returned by getResources().getDisplayMetrics().density;
这取决于你想要的行为,但你也可以考虑具有TextView的尺寸固定,并允许用户滚动文本:
This depends on your desired behavior, but you might also consider having the TextView size fixed, and allowing the user to scroll the text:
TextView tv = (TextView)findViewById(R.id.TextView02);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
这篇关于如何动态地设置TextView的高度机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!