现在的位置: 首页 > 综合 > 正文

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

2018年08月02日 ⁄ 综合 ⁄ 共 870字 ⁄ 字号 评论关闭

项目中遇到当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);
            }
        }

    }

抱歉!评论已关闭.