android maxLines ellipsize 配合使用失效,解决办法
来源:互联网 发布:淘宝达人个人介绍 编辑:程序博客网 时间:2024/05/05 01:39
首先,自定义一个TextView
package com.hx.readeralliance.customView;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.graphics.Canvas;import android.text.Layout;import android.text.Layout.Alignment;import android.text.StaticLayout;import android.text.TextUtils.TruncateAt;import android.util.AttributeSet;import android.widget.TextView; public class EllipsizingTextView extends TextView { private static final String ELLIPSIS = "..."; public interface EllipsizeListener { void ellipsizeStateChanged(boolean ellipsized); } private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>(); private boolean isEllipsized; private boolean isStale; private boolean programmaticChange; private String fullText; private int maxLines = -1; private float lineSpacingMultiplier = 1.0f; private float lineAdditionalVerticalPadding = 0.0f; public EllipsizingTextView(Context context) { super(context); } public EllipsizingTextView(Context context, AttributeSet attrs) { super(context, attrs); } public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void addEllipsizeListener(EllipsizeListener listener) { if (listener == null) { throw new NullPointerException(); } ellipsizeListeners.add(listener); } public void removeEllipsizeListener(EllipsizeListener listener) { ellipsizeListeners.remove(listener); } public boolean isEllipsized() { return isEllipsized; } @Override public void setMaxLines(int maxLines) { super.setMaxLines(maxLines); this.maxLines = maxLines; isStale = true; } public int getMaxLines() { return maxLines; } @Override public void setLineSpacing(float add, float mult) { this.lineAdditionalVerticalPadding = add; this.lineSpacingMultiplier = mult; super.setLineSpacing(add, mult); } @Override protected void onTextChanged(CharSequence text, int start, int before, int after) { super.onTextChanged(text, start, before, after); if (!programmaticChange) { fullText = text.toString(); isStale = true; } } @Override protected void onDraw(Canvas canvas) { if (isStale) { super.setEllipsize(null); resetText(); } super.onDraw(canvas); } private void resetText() { int maxLines = getMaxLines(); String workingText = fullText; boolean ellipsized = false; if (maxLines != -1) { Layout layout = createWorkingLayout(workingText); if (layout.getLineCount() > maxLines) { workingText = fullText.substring(0, layout.getLineEnd(maxLines - 1)).trim(); while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > maxLines) { int lastSpace = workingText.lastIndexOf(' '); if (lastSpace == -1) { break; } workingText = workingText.substring(0, lastSpace); } workingText = workingText + ELLIPSIS; ellipsized = true; } } if (!workingText.equals(getText())) { programmaticChange = true; try { setText(workingText); } finally { programmaticChange = false; } } isStale = false; if (ellipsized != isEllipsized) { isEllipsized = ellipsized; for (EllipsizeListener listener : ellipsizeListeners) { listener.ellipsizeStateChanged(ellipsized); } } } private Layout createWorkingLayout(String workingText) { return new StaticLayout(workingText, getPaint(), getWidth() - getPaddingLeft() - getPaddingRight(), Alignment.ALIGN_NORMAL, lineSpacingMultiplier, lineAdditionalVerticalPadding, false); } @Override public void setEllipsize(TruncateAt where) { // Ellipsize settings are not respected } }
然后,在布局文件中引用该TextView
<com.hx.readeralliance.customView.EllipsizingTextView android:id="@+id/tx_attention_des" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/contentMarginBottom" android:layout_marginLeft="@dimen/leftPadding" android:layout_marginRight="@dimen/rightPadding" android:layout_below="@+id/articlPic" android:textColor="@color/index_content" android:textSize="@dimen/index_content"/>
最后,在代码中设置最大的行数
viewHolder.txArticleDes.setMaxLines(5);viewHolder.txArticleDes.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
0 0
- android maxLines ellipsize 配合使用失效,解决办法
- android TextView 使用SpannableString数据源时,TextView的maxLines和ellipsize失效
- android:maxLines和android:ellipsize
- android:maxLines和android:ellipsize同时使用导致显示异常
- android:maxLines和android:ellipsize同时使用导致显示异常
- [bug]android:maxLines 和 android:ellipsize不能同时使用
- android:maxLines与android:ellipsize冲突
- android:maxLines="2" android:ellipsize="end"
- maxLines android:ellipsize="end" Spannable 互相之间存在冲突
- android TextView 使用SpannableString数据源时,TextView的ellipsize失效
- android ellipsize的使用
- android ellipsize的使用
- android:ellipsize的使用
- android:ellipsize的使用
- android:ellipsize的使用
- android:ellipsize的使用
- android:ellipsize的使用
- android:ellipsize的使用
- unity3D让物体惯性的旋转
- http://blog.csdn.net/tangaowen/article/details/8551762
- Xcode7.2注释插件失效
- hdu1150——最小点覆盖
- 他山之石-php缓存技术总结
- android maxLines ellipsize 配合使用失效,解决办法
- mysql 修改root密码
- 记一次淘宝首页奇葩的渲染问题
- 杭电OJ2680(dijkstra)choose the best route
- mfc下Teechart画饼图
- Arrays类应用
- windows主机开启openssl的方法
- EventBus
- 算法之优先队列