项目中最近需要实现类似微信朋友圈展开全文以及收起的效果,功能比较简单,用自定义view即可简单实现,直接上代码
/**布局文件也很简单:
* 可收起以及展开textView
*/
public class StretchyTextView extends LinearLayout implements View.OnClickListener {
/**
* 默认的最大行数
*/
private static final int MAX_LINES = 10;
//无状态
private static final int SPREADTEXT_STATE_NONE = 0;
//缩回状态
private static final int SPREADTEXT_STATE_RETRACT = 1;
//展开状态
private static final int SPREADTEXT_STATE_SPREAD = 2;
private int maxLineCount = MAX_LINES;
private int mState;
//实际的行数
private int actually_counts;
/**
* 显示内容的文本
*/
private TextView tv_stretchy_content;
/**
* 显示操作的文本
*/
private TextView tv_stretchy_bottom;
/**
* 显示按钮的图片
*/
private ImageView iv_stretchy_bottom;
private LinearLayout ll_stretchy_content;
private InnerRunnable runable;
private boolean flag = false;
public StretchyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//填充布局
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_tv_stretchy, this);
view.setPadding(0, 0, 0, 0);
tv_stretchy_content = (TextView) view.findViewById(R.id.tv_stretchy_content);
tv_stretchy_bottom = (TextView) view.findViewById(R.id.tv_stretchy_bottom);
iv_stretchy_bottom = (ImageView) view.findViewById(R.id.iv_stretchy_bottom);
ll_stretchy_content = (LinearLayout) view.findViewById(R.id.ll_stretchy_content);
ll_stretchy_content.setOnClickListener(this);
setBottomTextGravity(Gravity.CENTER_HORIZONTAL);
runable = new InnerRunnable();
}
/**
* 设置展开标识的显示位置
*
* @param gravity
*/
public void setBottomTextGravity(int gravity) {
ll_stretchy_content.setGravity(gravity);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!flag) {
flag = !flag;
Log.i("test","当前lines:"+tv_stretchy_content.getLineCount());
Log.i("test","实际行数:"+actually_counts);
if (actually_counts<=MAX_LINES && tv_stretchy_content.getLineCount() <=MAX_LINES) {
mState = SPREADTEXT_STATE_NONE;
ll_stretchy_content.setVisibility(View.GONE);
tv_stretchy_content.setMaxLines(MAX_LINES + 1);
} else {
post(runable);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public void onClick(View v) {
flag = false;
requestLayout();
}
/**
* 设置最大行数
*
* @param maxLineCount
*/
public void setMaxLineCount(int maxLineCount) {
this.maxLineCount = maxLineCount;
}
/**
* 设置文本内容颜色
*
* @param color
*/
public void setContentTextColor(int color) {
this.tv_stretchy_content.setTextColor(color);
}
/**
* 设置文本内容字体大小
*
* @param size
*/
public void setContentTextSize(float size) {
this.tv_stretchy_content.setTextSize(size);
}
/**
* 设置文本内容
*
* @param charSequence
*/
public final void setContent(CharSequence charSequence) {
tv_stretchy_content.setText(charSequence, TextView.BufferType.NORMAL);
post(new Runnable() {
@Override
public void run() {
actually_counts = tv_stretchy_content.getLineCount();
Log.i("test", "获取实际行数:" + actually_counts);
}
});
mState = SPREADTEXT_STATE_SPREAD;
flag = false;
requestLayout();
}
class InnerRunnable implements Runnable {
@Override
public void run() {
if (mState == SPREADTEXT_STATE_SPREAD) {
tv_stretchy_content.setMaxLines(MAX_LINES);
ll_stretchy_content.setVisibility(View.VISIBLE);
tv_stretchy_bottom.setText("显示全文");
iv_stretchy_bottom.setBackgroundResource(R.mipmap.buy_trailer_arrow_unselected);
mState = SPREADTEXT_STATE_RETRACT;
} else if (mState == SPREADTEXT_STATE_RETRACT) {
Log.i("test","需要收起");
tv_stretchy_content.setMaxLines(Integer.MAX_VALUE);
ll_stretchy_content.setVisibility(View.VISIBLE);
tv_stretchy_bottom.setText("收起");
iv_stretchy_bottom.setBackgroundResource(R.mipmap.buy_trailer_arrow_selected);
mState = SPREADTEXT_STATE_SPREAD;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>上面是一个TextView即时我们要显示的文本,下面的LinearLayout是展开和收起
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_stretchy_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:layout_gravity="center_vertical"
android:textSize="@dimen/collection_font_size"
android:text="文本内容"
/>
<LinearLayout
android:id="@+id/ll_stretchy_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:clickable="true"
>
<ImageView
android:id="@+id/iv_stretchy_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"
/>
<TextView
android:id="@+id/tv_stretchy_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:padding="2dp"
android:singleLine="true"
android:textColor="#ff576b95"
android:textSize="15.0dip"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
setContent() 设置文本内容
setContentTextSize() 设置文本字体大小
setContentTextColor 设置文本颜色
setMaxCount 设置TextView最大行数