【问题标题】:making textView wrap its text vertically使 textView 垂直包装其文本
【发布时间】:2016-09-27 12:51:13
【问题描述】:

我使用了这个代码:

Rect bounds=new Rect();
tv.getPaint().getTextBounds(text,0, text.length(),bounds);
float textWidth=bounds.width();
float textHeight=bounds.height();

它在水平方向上运行良好...... 垂直尺寸也可以... 但是文字的绘制位置比我预期的要低...

这些链接没有用:

get text height

auto scale textview text to fit within bounds

我应该改变什么?

view and its text

【问题讨论】:

    标签: android textview


    【解决方案1】:

    只需将您的布局参数设置为 WRAP_CONTENT 即可。如果您的文本在 LinearLayout 中,它看起来像这样:

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
    myTextView.setLayoutParams(params);
    

    如果您使用的是 XML 布局,只需将 TextView 的 layout_height 设置为 wrap_content。如果文本仍然低于您的预期,请检查 TextView 的填充设置。

    【讨论】:

      猜你喜欢
      • 2020-06-18
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2012-05-02
      相关资源
      最近更新 更多