Android可收缩/扩展的TextView

       在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容。
这样的设计有一定的优越性,毕竟用户的时间有限,注意力和关注力也有限,在使用APP时候,用户需要在最短时间内尽可能快速浏览和查阅到更主要内容,而不是一大堆泛泛而谈的文字内容。
在Android原生的TextView的基础上,我自己写了一个可收缩/扩展的TextView:PhilExpandableTextView。

实现原理:核心是控制TextView的max lines。在TextView的初始化阶段但尚未绘制出View的时候,使用ViewTreeObserver,监听onPreDraw事件,获取TextView正常显示需要显示的总行数,但只给TextView设置最大运行的行数(小于总行数),从而造成TextView的收缩摘要效果,当用户通过按钮或其他方式扩展时候,把TextView的最大行数设置为正常显示完全的行数+1(+1是保持余量,避免不足)。

如下图:



一、测试的Java代码

1、mainActivity

package com.hxzy.expandabletextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private String text_str = "";
	private String buttonText1 = "显示全文";
	private String buttonText2 = "收起";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 测试的字符串
		for (int i = 0; i < 200; i++) {
			text_str = text_str + "" + i;
		}

		final ExpandableTextView text = (ExpandableTextView) findViewById(R.id.text);
		text.setText(text_str);

		final Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean b = text.getExpandableStatus();
				b = !b;
				text.setExpandalbe(b);

				// 设置按钮文本
				if (b == false) {
					button.setText(buttonText1);

				} else {
					button.setText(buttonText2);

				}

			}
		});
	}
}

2、ExpandableTextView (核心代码部分)

package com.hxzy.expandabletextview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnDrawListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.TextView;

public class ExpandableTextView extends TextView{

	
	// 最大的行,默认只显示3行 
	private final int MAX=3;
  
    // 如果完全伸展需要多少行?  
	private int lines;
	private ExpandableTextView mExpandableTextView;
	
	// 标记当前TextView的展开/收缩状态  
    // true,已经展开  
    // false,以及收缩 
	private boolean expandableStatus=false;
	
	//构造函数用此下两个参数的
	public ExpandableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mExpandableTextView=this;
		init();
	}

	private void init() {
		 // ViewTreeObserver View观察者,在View即将绘制但还未绘制的时候执行的,在onDraw之前  
		ViewTreeObserver mViewTreeObserver=this.getViewTreeObserver();
		mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
			
			@Override
			public boolean onPreDraw() {
				// 避免重复监听
				mExpandableTextView.getViewTreeObserver().removeOnPreDrawListener(this);
				lines=getLineCount();
				return true;
			}
		});

		setExpandalbe(false);
	}

	 // 是否展开或者收缩,  
    // true,展开;  
    // false,不展开  
	public void setExpandalbe(boolean isExpand){
		if(isExpand){
			setMaxLines(lines+1);
		}else{
			setMaxLines(MAX);
		}
		expandableStatus=isExpand;
	}
	
	public boolean getExpandableStatus(){
		
		return expandableStatus;
		
	}

}

二、 MainActivity.java需要的布局文件:

<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"
    tools:context="com.hxzy.expandabletextview.MainActivity" >
    
    <com.hxzy.expandabletextview.ExpandableTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#03a9f4"
        ></com.hxzy.expandabletextview.ExpandableTextView>

    <Button
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示全文"
         />

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值