textview.getLineCount 在 android 中始终为 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3528790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:37:25  来源:igfitidea点击:

textview.getLineCount always 0 in android

androidtextview

提问by neha

I'm trying to dynamically resize my textview but getlinecount() method always returns me 0 even after settext() and invalidate(). I'm using the following code:

我正在尝试动态调整文本视图的大小,但即使在 settext() 和 invalidate() 之后,getlinecount() 方法也始终返回 0。我正在使用以下代码:

if (convertView == null) {
    convertView = lInflater.inflate(R.layout.listview, null);
    holder = new ViewHolder();
    holder.text2 = (TextView)convertView.findViewById(R.id.TextView02);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder)convertView.getTag();
}

holder.text2.setText(arr2[position]);
holder.text2.invalidate();

int lineCnt = holder.text2.getLineCount();

holder is a static class as follows:

持有人是一个静态类,如下所示:

static class ViewHolder {
    TextView text2;
}

holder contains non null text2 and the content set is also non null.

持有者包含非空 text2 并且内容集也非空。

Can anybody please help?

有人可以帮忙吗?

Thanx in advance.

提前谢谢。

回答by Scott

I know this question is quite old, but in case anyone comes here looking for the actual answer:

我知道这个问题已经很老了,但万一有人来这里寻找实际答案:

holder.text2.setText(arr2[position]);
holder.text2.post(new Runnable() {
    @Override
    public void run() {
        int lineCnt = holder.text2.getLineCount();
        // Perform any actions you want based on the line count here.
    }
});

回答by Nima K

It should be done on UI Thread by post method.

它应该通过 post 方法在 UI 线程上完成。

textView.post(new Runnable() {
    @Override
    public void run() {
        Log.v("Line count: ", textView.getLineCount()+"");
    }
});

回答by Nativ

In order to fix this issue apply the following lines:

为了解决此问题,请应用以下几行:

textView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (textView.getLineCount() > 1) {
            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }
});

The OnGlobalLayoutListenerwill be called after every change to the TextView(after measuring it, drawing it, ..). Here you can catch changes to your TextViewbefore it'll be drawn to the screen and do whatever you need with it.

OnGlobalLayoutListener会每次更改后调用TextView(测量它,绘图之后,..)。在这里,您可以TextView在将更改绘制到屏幕之前捕获对您的更改,并使用它执行任何您需要的操作。

The last line in the code is to remove the listener, it's important since we don't want to continue catching each layout change.

代码中的最后一行是删除侦听器,这很重要,因为我们不想继续捕获每个布局更改。

回答by DeRagan

getLineCount() will give you the correct number of lines only after a layout pass. That means the TextView must have been drawn at least once. I assume that at this point of time your Textview is not drawn hence you are getting 0 as the line count

getLineCount() 只有在布局通过后才会为您提供正确的行数。这意味着 TextView 必须至少被绘制一次。我假设此时您的 Textview 未绘制,因此您的行数为 0

回答by LinWei

actually TextView.getLineCount() rely on TextView.mLayout.getLineCount but TextView.mLayout is lazy init before onMeasure, you can do like this:

实际上 TextView.getLineCount() 依赖于 TextView.mLayout.getLineCount 但 TextView.mLayout 在 onMeasure 之前是惰性初始化,你可以这样做:

       if (holder.contentTV.getLayout() == null) {
            holder.contentTV.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    holder.contentTV.getLineCount();
                    holder.contentTV.getViewTreeObserver().removeOnPreDrawListener(this);
                    return true;
                }
            });
        } else {
            holder.contentTV.getLineCount();
        }

回答by BlacklegSanji

I made an asynctask class and did my getLineCount()-related tasks in its onPostExecute method.

我创建了一个 asynctask 类,并在其 onPostExecute 方法中执行了与 getLineCount() 相关的任务。

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new myAsyncTask().execute(null, null, null);
}

private class myAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(final Void... params) {
        // It's okay to leave this as it is
        return null;
    }

    @Override
    protected void onPostExecute(final Void result) {
        super.onPostExecute(result);
        //DO YOUR TASK HERE, getLineCount(), etc.       
    }
}