在listview中要获得某个textView的行数:
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.text2.getLineCount();为0
具体原因为:
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
针对上面的问题我们可以在一个非主线程即非UI线程中来获得lineCount.
可以这样做:
....................
else{ holder = (ViewHolder)convertView.getTag(); } holder.text2.setText(arr2[position]); holder.text2.invalidate();
..................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.new myAsyncTask().execute(null, null, null);
//在非主线程调用getLinrCount()int lineCnt = holder.text2.getLineCount();
} }