通过 TextView.getLineCount() 获取当前文字行数
XML代码

adapter 代码


因为TextView 设置text 需要时间,所以在adapter里面获取 lineCount 数需要通过以下方法
tvContent?.viewTreeObserver?.addOnGlobalLayoutListener{
if (tvContent?.maxLines ==4) {
contentOpen?.visibility = View.GONE
contentClose?.visibility = View.GONE
if (tvContent?.lineCount >3) {
// 显示 展开
contentOpen?.visibility = View.VISIBLE
}
// 设置最大行数超出三行显示为...
tvContent?.maxLines =3
}
}
//展开点击事件
contentOpen?.setOnClickListener{
tvContent?.maxLines =999
contentOpen?.visibility = View.GONE
contentClose?.visibility = View.VISIBLE
}
//收起点击事件
contentClose?.setOnClickListener{
tvContent?.maxLines =3
contentOpen?.visibility = View.VISIBLE
contentClose?.visibility = View.GONE
}
//以上代码是kotlin代码
网友评论