1,ellipsize等于marquee,跑马灯效果
http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EEEEEE" >
android:id="@+id/textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#EEEE00" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:singleLine="true" android:text="@string/hello_world" android:textSize="15dp" android:textColor="#FF0000" />
运行效果是:
从右向左循环滚动。
2,ellipsize等于end
android:id="@+id/textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#EEEE00" android:ellipsize="end" android:singleLine="true" android:text="@string/hello_world" android:textSize="15dp" android:textColor="#FF0000" />
3,ellipsize等于start
android:id="@+id/textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#EEEE00" android:ellipsize="start" android:singleLine="true" android:text="@string/hello_world" android:textSize="15dp" android:textColor="#FF0000" />
4,ellipsize等于middle
android:id="@+id/textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#EEEE00" android:ellipsize="middle" android:singleLine="true" android:text="@string/hello_world" android:textSize="15dp" android:textColor="#FF0000" />
5,ellipsize等于none
android:id="@+id/textview" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#EEEE00" android:ellipsize="none" android:singleLine="true" android:text="@string/hello_world" android:textSize="15dp" android:textColor="#FF0000" />
6,上述5种用法在代码中设置也可以达到效果
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setEllipsize(TruncateAt.END);
textView.setEllipsize(TruncateAt.START);
textView.setEllipsize(TruncateAt.MIDDLE);
或者,
tv.setEllipsize(TruncateAt.valueOf("END"));
tv.setEllipsize(TruncateAt.valueOf("START"));
tv.setEllipsize(TruncateAt.valueOf("MIDDLE"));
tv.setEllipsize(TruncateAt.valueOf("MARQUEE"));
在代码中设置也可以达到效果
class WidgetUtil
{
public static
void handleTextView(TextView textView, String
textStr)
{
textView.setText(textStr);//首先要赋值一次,让系统自动处理,产生自动换行
ViewTreeObserver vto = textView.getViewTreeObserver();
MyOnGlobalLayoutListenerlayoutListener = new
MyOnGlobalLayoutListener(textView, textStr);
vto.addOnGlobalLayoutListener((OnGlobalLayoutListener)
layoutListener);
}
}
class MyOnGlobalLayoutListenerimplements
OnGlobalLayoutListener
{
private TextView textView;
private String textValue;
public MyOnGlobalLayoutListener(TextView textView, String
textValue)
{
this.textView = textView;
this.textValue = textValue;
}
@Override
public void onGlobalLayout()
{
ViewTreeObserver obs = textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (textView.getLineCount() >
1)//如果一行显示不下而自动换行,所以要在前台文件作修改,去掉singleLine=true,否则该条件不会成立。
{
int lineEndIndex =
this.textView.getLayout().getLineEnd(0);//获取被截断的字符长度
String text = textValue.subSequence(0, lineEndIndex - 3) +
"...";//手动加上省略号
textView.setText(text);
}
}
}