android:maxLines和android:ellipsize同时使用的时候没有显示省略号,最后加上singleLine为false才显示出来。如下:android:maxLines="4"
android:ellipsize="end"
android:singleLine="false"
只有end管用,其他的还是不能显示,有待研究。其中有一个作者提出了一下方法,作为参考:
android:maxLines="4"
android:ellipsize="end"
android:singleLine="false"
网址:http://www.jb51.net/article/41237.htm
布局中代码:
<TextView
android:id="@+id/businesscardsingle_content_abstract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.0"
android:lines="6"
android:text="@string/agrinbusiness_content"
android:textColor="#7f7f7f"
android:textSize="13sp" />
<TextView
android:id="@+id/businesscardsingle_content_abstract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.0"
android:lines="6"
android:text="@string/agrinbusiness_content"
android:textColor="#7f7f7f"
android:textSize="13sp" />
java代码
ViewTreeObserver observer = textAbstract.getViewTreeObserver(); //textAbstract为TextView控件
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = textAbstract.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if(textAbstract.getLineCount() > 6) //判断行数大于多少时改变
{
int lineEndIndex = textAbstract.getLayout().getLineEnd(5); //设置第六行打省略号
String text = textAbstract.getText().subSequence(0, lineEndIndex-3) +"...";
textAbstract.setText(text);
}
}
});
ViewTreeObserver observer = textAbstract.getViewTreeObserver(); //textAbstract为TextView控件
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = textAbstract.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if(textAbstract.getLineCount() > 6) //判断行数大于多少时改变
{
int lineEndIndex = textAbstract.getLayout().getLineEnd(5); //设置第六行打省略号
String text = textAbstract.getText().subSequence(0, lineEndIndex-3) +"...";
textAbstract.setText(text);
}
}
});