作者:Yogi
前言:当在界面观察时,明明看到TextView上的字体已经超出显示范围,却不发生自滚动。但是,你看到它超出,不一定真的超出哦。此篇文章告诉你,如何看实实在在的TextView的显示范围以及文本所占宽度。
TextPaint paint = mTvProgramName.getPaint();paint.setTextSize(mTvProgramName.getTextSize());// paint.measureText(mTvProgramName.getText().toString()); //这个方法能把文本所占宽度衡量出来.Log.i(TAG, "getFocus paint.measureText(mTvProgramName.getText().toString())="+ paint.measureText(mTvProgramName.getText().toString()));mTvProgramName.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {@Overridepublic void onLayoutChange(final View v, final int left, final int top, final int right, final int bottom, final int oldLeft, final int oldTop, final int oldRight,final int oldBottom) {//在此,得到TextView控件的宽度Log.i(TAG, "onLayoutChange mTvProgramName.getWidth()=" + v.getWidth());}});