項目中遇到當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); } } }