abdroid关于TextView.getLineCount() 为0

在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();
        
        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. 

      //在非主线程调用getLinrCount()
int lineCnt = holder.text2.getLineCount();

    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值