android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
2014-11-25 14:10
921 查看
这篇文章介绍了android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法,有需要的朋友可以参考一下
布局文件中的TextView属性
复制代码代码如下:
<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);
}
}
});
布局文件中的TextView属性
复制代码代码如下:
<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);
}
}
});
相关文章推荐
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
- android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法
- android TextView多行文本(超过3行)使用ellipsize="end"属性无效问题的解决方法
- android TextView多行文本(超过3行)使用ellipsize属性无效
- Android使用TextView,设置onClick属性无效的解决方法
- Android使用TextView,设置onClick属性无效解决的方法
- Android使用TextView,设置onClick属性无效解决办法
- Android使用TextView,设置onClick属性无效解决办法
- 使用android-support-multidex解决Android方法数超过65535的问题
- Android TextView中部分文本可变,而且颜色不一致的解决方法
- 解决TextView数据不能更新的问题以及Android Button事件响应函数的两种方法
- Android TextView使用SpannableString设置复合文本的方法详解
- Android AutoWrapTextView中英文排版问题的解决方法
- TextView设置android:ellipsize="marquee"属性,无法实现跑马灯效果的解决方法
- Android textview 加载html语言(解决超链接不能使用的问题)
- Android编程中TextView宽度过大导致Drawable无法居中问题解决方法
- android的textview显示音标问题解决方法
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法