引言
有段时间没更新博客了,一个是有点忙,还有就是目前所在地区把csdn的博客墙了,哎一言难尽。言归正传,最近项目用到了ExpandableTextView效果,发现有的时候收缩会失效,自己据琢磨一下看看TextViews有没有属性自己实现一个。
程序机构
主程序
package com.example.textview;
import com.example.hhhh.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
/**
* @Fields TextView 闭合展开: 默认展示最大行数3行
*/
public static final int MAX_LINE = 3;
/**
* @Fields TextView 闭合展开: 收起状态
*/
public static final int SHRINK_UP_STATE = 1;
/**
* @Fields TextView 闭合展开: 展开状态
*/
public static final int SPREAD_STATE = 2;
/**
* @Fields mState : 默认收起状态
*/
private static int mState = SHRINK_UP_STATE;
private ImageView indicatorImage;
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
content = (TextView)this.findViewById(R.id.content);
indicatorImage = (ImageView)this.findViewById(R.id.image);
content.setOnClickListener(this);
indicatorImage.setOnClickListener(this);
content.setText(" 斑驳的旧时光里,总有一些或深或浅温暖人心的馨香,"
+ "让人舍不得遗忘。潮湿的过往,不经意间邂逅了谁的眼眸;文字里泛舟,"
+ "又与谁的倩影不期而遇?淡淡的墨香中,谁在翩然靠近,袅袅娜娜,直抵心间?一切,"
+ "都那么美,那么惆怅,这不仅仅是文字的描摹,其外还有收藏着的心思与情感。的确,"
+ "光阴里曾经明媚或暗淡的过往,总是伴着美丽的忧伤与成长的阵痛;日渐淡薄的往昔,"
+ "也总能或多或少的在心里留下一丝清雅的执念。收藏,是因为骨子里是个守旧的人;"
+ "收藏,更是因为心里有值得铭记的感动。今生,心为笔,写下满笺素心浅事,不求浮华,"
+ "但求安然;今生,心作尘,用绵长的时光,写下一份浅淡,缓缓落墨,轻轻收笔。");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.image:
case R.id.content:
if (mState == SPREAD_STATE) {
content.setMaxLines(MAX_LINE);
content.requestLayout();
mState = SHRINK_UP_STATE;
indicatorImage.setImageResource(R.drawable.icon_content_open);
} else if (mState == SHRINK_UP_STATE) {
content.setMaxLines(Integer.MAX_VALUE);
content.requestLayout();
mState = SPREAD_STATE;
indicatorImage.setImageResource(R.drawable.icon_content_close);
}
break;
default:
break;
}
}
}
xml页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:ellipsize="end"
android:maxLines="3"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:singleLine="false"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/content"
android:layout_marginRight="8dp"
android:scaleType="fitXY"
android:src="@drawable/icon_content_open" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/image"
android:background="@drawable/line_bg" />
</RelativeLayout>
</LinearLayout>
运行效果
dome:资源