Android擷取TextView行數方法getLineCount()傳回值為0

來源:互聯網
上載者:User

項目中遇到當TextView顯示的資料不超過3行的時候,不顯示下面的展開按鈕,這時候就必須要擷取到此時TextView的行數,查看api發現了getLineCount()方法,當我興高采烈地使用時,卻放心傳回值總為空白,後來查Google才發現說是要先把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);             }         }       }  

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.