android自定义实现textview限制多少行数以后以省略号结尾。 ... 这种省略号不是
2018-03-13 17:46
801 查看
系统的省略号是居中的。如果有人不喜欢想要靠下的那种,那你就来对了,不说了,上代码了,全程无尿点,支持超链图文package com.yilvs.views;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.DynamicLayout;
import android.text.StaticLayout;
import android.util.AttributeSet;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.text.DateFormat;
/**
* Created by yilv on 2018/3/12.
*/
@SuppressLint("AppCompatCustomView")
public class EllipsizeTextView extends TextView {
public EllipsizeTextView(Context context) {
super(context);
}
public EllipsizeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
@SuppressLint("NewApi")
public EllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
StaticLayout layout = null;
Field field = null;
try {
Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout");
staticField.setAccessible(true);
layout = (StaticLayout) staticField.get(DynamicLayout.class);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (layout != null) {
try {
field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount");
field.setAccessible(true);
field.setInt(layout, getMaxLines());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (layout != null && field != null) {
try {
field.setInt(layout, Integer.MAX_VALUE);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.DynamicLayout;
import android.text.StaticLayout;
import android.util.AttributeSet;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.text.DateFormat;
/**
* Created by yilv on 2018/3/12.
*/
@SuppressLint("AppCompatCustomView")
public class EllipsizeTextView extends TextView {
public EllipsizeTextView(Context context) {
super(context);
}
public EllipsizeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
@SuppressLint("NewApi")
public EllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
StaticLayout layout = null;
Field field = null;
try {
Field staticField = DynamicLayout.class.getDeclaredField("sStaticLayout");
staticField.setAccessible(true);
layout = (StaticLayout) staticField.get(DynamicLayout.class);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (layout != null) {
try {
field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount");
field.setAccessible(true);
field.setInt(layout, getMaxLines());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (layout != null && field != null) {
try {
field.setInt(layout, Integer.MAX_VALUE);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- [置顶] android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- Android自定义AutoCompleteTextView实现自动补全Email
- android listView 自定义布局结合CheckedTextView实现多选
- Android中的TextView实现多行显示省略号 .
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- 解析在Android中为TextView增加自定义HTML标签的实现方法
- android TextView 小属性(积累)---------限制TextView行数后,文字未显示完全出现...
- Android中如何让TextView显示指定的行数并且多出的部分显示省略号
- android自定义view 实现TextView 中文粗体
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
- Android-自定义TextView和异步加载图片的实现
- Android中的TextView实现多行显示省略号以及下划线的实现
- Android UI设计之<四>自定义TextView属性,实现带边框效果的TextView
- Android TextView自定义实现@用户跳转
- Android-自定义TextView和异步加载图片的实现
- Android TextView 实现文字过长省略号.....
- Android中的TextView实现多行显示省略号
- Android 自定义AutoCompleteTextView实现显示所有项