获得TextView的真实行数调用getLineCount()方法时,获得的始终为0 ,关于如何获得真实的行数值,有以下解决方案:
1,绘画完成之前调用的,在这里面可以获取到行数,当然也可以获取到宽高等信息。
ViewTreeObserver vto = mContent.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if(mContent.getLineCount() == 1){
} else {
}
return false;
}
});
2, 绘画完成之后调用的,在这里面可以获取到行数,当然也可以获取到宽高等信息。
mContent.setText(barModel.getContentTitle());
mContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if(mContent.getLineCount() == 1){
Log.e("TAG","---------"+mContent.getLineCount());
} else {
}
}
});
3,
使用 post,绘制完成在调用 getLineCount()
mContent.post(new Runnable() {
@Override
public void run() {
if(mContent.getLineCount() == 1){
mContent.setPadding(0,(int)mContext.getResources().getDimension(R.dimen.space_12),0,0);
mReadNum.setPadding(0,0,0,(int)mContext.getResources().getDimension(R.dimen.space_12));
mCollectionNum.setPadding(0,0,0,(int)mContext.getResources().getDimension(R.dimen.space_12));
} else {
mContent.setPadding(0,0,0,0);
mReadNum.setPadding(0,0,0,0);
mCollectionNum.setPadding(0,0,0,0);
}
}
});
经过测试三种方案均可行,但是如果是listview中TextView,根据LineCount切换位置,会出现
TextView复用,导致TextView位置在滑动速度快的情况下位置瞬间混乱,采用第三种方案没有此问题,至于采用前两种方案出现问题的原因不得而知,有遇到这种情况的可以一起探讨一下原因。
以上原文链接:https://blog.youkuaiyun.com/zhzzhz123456/article/details/52925753
在绘制前获取textView的行数可以使用StaticLayout,TextView内部实现行数计算也是使用的StaticLayout,所以new一个StaticLayout来协助计算出TextView会占用的行数,以下工具类
public class TextViewLinesUtil {
public static int getTextViewLines(TextView textView, int textViewWidth) {
int width = textViewWidth - textView.getCompoundPaddingLeft() - textView.getCompoundPaddingRight();
StaticLayout staticLayout;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
staticLayout = getStaticLayout23(textView, width);
} else {
staticLayout = getStaticLayout(textView, width);
}
int lines = staticLayout.getLineCount();
int maxLines = textView.getMaxLines();
if (maxLines > lines) {
return lines;
}
return maxLines;
}
/**
* sdk>=23
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private static StaticLayout getStaticLayout23(TextView textView, int width) {
StaticLayout.Builder builder = StaticLayout.Builder.obtain(textView.getText(),
0, textView.getText().length(), textView.getPaint(), width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setTextDirection(TextDirectionHeuristics.FIRSTSTRONG_LTR)
.setLineSpacing(textView.getLineSpacingExtra(), textView.getLineSpacingMultiplier())
.setIncludePad(textView.getIncludeFontPadding())
.setBreakStrategy(textView.getBreakStrategy())
.setHyphenationFrequency(textView.getHyphenationFrequency())
.setMaxLines(textView.getMaxLines() == -1 ? Integer.MAX_VALUE : textView.getMaxLines());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setJustificationMode(textView.getJustificationMode());
}
if (textView.getEllipsize() != null && textView.getKeyListener() == null) {
builder.setEllipsize(textView.getEllipsize())
.setEllipsizedWidth(width);
}
return builder.build();
}
/**
* sdk<23
*/
private static StaticLayout getStaticLayout(TextView textView, int width) {
return new StaticLayout(textView.getText(),
0, textView.getText().length(),
textView.getPaint(), width, Layout.Alignment.ALIGN_NORMAL,
textView.getLineSpacingMultiplier(),
textView.getLineSpacingExtra(), textView.getIncludeFontPadding(), textView.getEllipsize(),
width);
}
}