项目过程中可能会用到可以折叠和展开的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 中添加以下内容
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ExpandableTextView"> <attr name="trimLength" format="integer" /> </declare-styleable> </resources>
然后在你的布局文件中引用如下代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ExpandableTextActivity" > <com.example.expandtextviewdemo.ExpandableTextView android:id="@+id/lorem_ipsum" android:textSize="20sp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
最后 , 在你的acitivty中测试一下
package com.example.expandtextviewdemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class ExpandableTextActivity extends Activity { @Override protected 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); } @Override public 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; } }