【问题标题】:Need to getLineCount form before rendering TextView渲染TextView前需要getLineCount表单
【发布时间】:2014-03-12 08:49:12
【问题描述】:

我想在我的适配器的 getview 函数中渲染之前设置 TextView 的高度。 getLineCount 中的结果始终为 0...

public View getView(LayoutInflater inflater, View convertView)
{
    textView = (TextView) rowView.findViewById(R.id.text);
    textView.setText(strText);
    int height_in_pixels = textView.getLineCount() * textView.getLineHeight();// getLineCount is always 0
    textView.setHeight(height_in_pixels);
}

我也在想也许可以计算strText 中的字符数,然后根据字符数设置我的高度..

感谢您的帮助

【问题讨论】:

  • TextView 在渲染文本之前不知道实际的行数,因此您遇到了典型的鸡蛋问题。您可以尝试使用 View.INVISIBLE,渲染它,运行您的代码,然后将可见性更改为 View.VISIBLE
  • 如何在 getView() 函数中渲染它?

标签: android android-listview textview


【解决方案1】:

那是因为内部布局没有设置,你必须在内部布局建立后调用它 - 你可以 例如在调用setText() 后发布runnable - 这样做你确保 内部布局搭建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2013-03-18
    • 2020-02-12
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多