Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

textview.getLineCount always 0 in android

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:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...