android textView 摺疊 展開 ExpandableTextView

來源:互聯網
上載者:User

項目過程中可能會用到可以摺疊和展開的TextView , 這裡給出一種實現思路,自訂控制項。

package com.example.expandtextviewdemo;import android.content.Context;import android.content.res.TypedArray;import android.text.SpannableStringBuilder;import android.util.AttributeSet;import android.view.View;import android.widget.TextView;public class ExpandableTextView extends TextView {    private static final int DEFAULT_TRIM_LENGTH = 200;    private static final String ELLIPSIS = ".....";     private CharSequence originalText;    private CharSequence trimmedText;    private BufferType bufferType;    private boolean trim = true;    private int trimLength;     public ExpandableTextView(Context context) {        this(context, null);    }     public ExpandableTextView(Context context, AttributeSet attrs) {        super(context, attrs);         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);        typedArray.recycle();         setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                trim = !trim;                setText();                requestFocusFromTouch();            }        });    }     private void setText() {        super.setText(getDisplayableText(), bufferType);    }     private CharSequence getDisplayableText() {        return trim ? trimmedText : originalText;    }     @Override    public void setText(CharSequence text, BufferType type) {        originalText = text;        trimmedText = getTrimmedText(text);        bufferType = type;        setText();    }     private CharSequence getTrimmedText(CharSequence text) {        if (originalText != null && originalText.length() > trimLength) {            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);        } else {            return originalText;        }    }     public CharSequence getOriginalText() {        return originalText;    }     public void setTrimLength(int trimLength) {        this.trimLength = trimLength;        trimmedText = getTrimmedText(originalText);        setText();    }     public int getTrimLength() {        return trimLength;    }}

需要在你的attr.xml 中添加以下內容

                

然後在你的布局檔案中引用如下代碼:

    

最後 , 在你的acitivty中測試一下


package com.example.expandtextviewdemo;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class ExpandableTextActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_expandable_text);String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "+ "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae "+ "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec "+ "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam "+ "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel "+ "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla "+ "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam"+ " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante"+ " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem "+ "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum"+ " sed laoreet suscipit. ";ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum);expandableTextView.setText(yourText);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.expandable_text, menu);return true;}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.