yongyong_21 2012-10-11 09:40 采纳率: 100%
浏览 31263
已采纳

动态的设置textview的高度

我想在应用程序中给textview动态的设置文本,所以我想实现textview能动态调整大小。我按照以下代码设置,但是没设置成功:

<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行,里面的文本也不能消减。

  • 写回答

2条回答 默认 最新

  • hxn_217 2012-10-12 03:26
    关注

    像znl_12说的,除非你删除android:maxLines="4",否则超过4行,上面的代码就可以忽略了。
    要这样设置高度:

    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;
    

    也可以用滚动条的方式调节text的大小:

    TextView tv = (TextView)findViewById(R.id.TextView02);
    tv.setMovementMethod(ScrollingMovementMethod.getInstance());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型