我希望在三行文本后添加“更多”功能.该文本包含超过10行的描述.所以我们决定在三行文字之后添加“更多”.喜欢:
当文本显示完整描述时,它应该在文本末尾显示“Less”按钮,再次压缩textview.
解决方法:
试试这可能会帮助你和我一起工作.
public class MainActivity extends Activity { TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); makeTextViewResizable(tv, 3, "View More", true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public static void makeTextViewResizable(final TextView tv, final int maxLine, final String expandText, final boolean viewMore) { if (tv.getTag() == null) { tv.setTag(tv.getText()); } ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @Override public void onGlobalLayout() { String text; int lineEndindex; ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); if (maxLine == 0) { lineEndindex = tv.getLayout().getLineEnd(0); text = tv.getText().subSequence(0, lineEndindex - expandText.length() + 1) + " " + expandText; } else if (maxLine > 0 && tv.getLineCount() >= maxLine) { lineEndindex = tv.getLayout().getLineEnd(maxLine - 1); text = tv.getText().subSequence(0, lineEndindex - expandText.length() + 1) + " " + expandText; } else { lineEndindex = tv.getLayout().getLineEnd(tv.getLayout().getLineCount() - 1); text = tv.getText().subSequence(0, lineEndindex) + " " + expandText; } tv.setText(text); tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText( addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, lineEndindex, expandText, viewMore), BufferType.SPANNABLE); } }); } private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv, final int maxLine, final String spanableText, final boolean viewMore) { String str = strSpanned.toString(); SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned); if (str.contains(spanableText)) { ssb.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { tv.setLayoutParams(tv.getLayoutParams()); tv.setText(tv.getTag().toString(), BufferType.SPANNABLE); tv.invalidate(); if (viewMore) { makeTextViewResizable(tv, -1, "View Less", false); } else { makeTextViewResizable(tv, 3, "View More", true); } } }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0); } return ssb; } }
更新:从spaneble文本中删除UnderLine
1)创建自定义ClickableSpan
public class MySpannable extends ClickableSpan { private boolean isUnderline = false; /** * Constructor */ public MySpannable(boolean isUnderline) { this.isUnderline = isUnderline; } @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(isUnderline); ds.setColor(Color.parseColor("#343434")); } @Override public void onClick(View widget) { } }
2)更改addClickablePartTextViewResizable()方法
private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv, final int maxLine, final String spanableText, final boolean viewMore) { String str = strSpanned.toString(); SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned); if (str.contains(spanableText)) { ssb.setSpan(new MySpannable(false){ @Override public void onClick(View widget) { tv.setLayoutParams(tv.getLayoutParams()); tv.setText(tv.getTag().toString(), BufferType.SPANNABLE); tv.invalidate(); if (viewMore) { makeTextViewResizable(tv, -1, "View Less", false); } else { makeTextViewResizable(tv, 3, "View More", true); } } }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0); } return ssb; }