如何动态设置textview高度android [英] How to dynamically set textview height android

查看:90
本文介绍了如何动态设置textview高度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?

提前感谢.

推荐答案

正如康斯坦丁所说,这段代码在超过 4 行后可能会被忽略,除非你删除 android:maxLines="4"

As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"

这是在代码中设置高度的方式:

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().density; 返回的值

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高度android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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