android– 在3行之后在textview末尾添加“查看更多”

android– 在3行之后在textview末尾添加“查看更多”,第1张

概述参见英文答案>Android–ExpandableTextViewwithAnimation                                    12个我希望在三行文本后添加“更多”功能.该文本包含超过10行的描述.所以我们决定在三行文字之后添加“更多”.喜欢:当文本显示完整描述

参见英文答案 > Android – Expandable TextView with Animation                                    12个
我希望在三行文本后添加“更多”功能.该文本包含超过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;    }

输出:

总结

以上是内存溢出为你收集整理的android – 在3行之后在textview末尾添加“查看更多”全部内容,希望文章能够帮你解决android – 在3行之后在textview末尾添加“查看更多”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存