Android获取TextView行数方法getLineCount()返回值为0

项目中遇到当TextView显示的数据不超过3行的时候,不显示下面的展开按钮,这时候就必须要获取到此时TextView的行数,查看api发现了getLineCount()方法,当我兴高采烈地使用时,却放心返回值总为空,后来查谷歌才发现说是要先把TextView完全画出来,才能获取行数,并说解决方法就是开启异步去获取,下面是具体的方法:

 private class MyOpenTask extends AsyncTask<Integer, Integer, Integer> {
        private int[] location = new int[2];

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        public void start() {
            execute(0);
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            return 1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            int linecount = mVideoDescription.getLineCount();
            android.util.Log.e("MvDetail", "linecount1::"+linecount);
            if(linecount>3){
                openTV.setVisibility(View.VISIBLE);
                openIV.setVisibility(View.VISIBLE);
                openTV.setText("展开全部");
                mVideoDescription.setClickable(true);
                openFlag = false;
            }else{
                openTV.setVisibility(View.GONE);
                openIV.setVisibility(View.GONE);
                mVideoDescription.setClickable(false);
            }
        }

    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值