Textview文字的收起与展开功能

时间:2021-01-31 19:36:58

实现原理:用两个Textview来控制显示,默认显示短文字(限制行数)的Textview,当点击展开时,显示全部文字的Textview。


public class MainActivity extends Activity {

private TextView shortTxt, longTxt, txtOpenOrClose;
private LinearLayout linearLayout;
//是否初始化,防止无限制的判断信息是否过长
private boolean isInit=false;
private static final String OPEN = "展开";
private static final String CLOSE = "收起";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initViews();
String str = "《幻影车神3》是一部在2013年12月20日圣诞节上映的印度电影·由阿米尔·汗、卡特莉娜·卡芙、"
+ "塔布莱特·贝赛尔等宝莱坞明星主演,Vijay Krishna Acharya执导。影片讲述了一个原本在马戏团混日子的男人,"
+ "为了给父亲报仇,决心去芝加哥一家腐败银行搞破坏,两位孟买警察对他一路追踪。";
// String str="这句话很短!";
shortTxt.setText(str);
longTxt.setText(str);
}

private void initViews() {
// TODO Auto-generated method stub
linearLayout = (LinearLayout) findViewById(R.id.id_linearLayout);
shortTxt = (TextView) findViewById(R.id.id_textview);
longTxt = (TextView) findViewById(R.id.id_textview2);
txtOpenOrClose = (TextView) findViewById(R.id.id_openOrClose);

txtOpenOrClose.setVisibility(View.GONE);
txtOpenOrClose.setOnClickListener(clickListener);
//添加回调方法,此方法在每次绘制视图的时候回调,如点击展开时就会调用此方法。
ViewTreeObserver observer = linearLayout.getViewTreeObserver();
observer.addOnPreDrawListener(onPreDrawListener);
}


/**
*按钮单击事件
*/
private OnClickListener clickListener = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//如果显示的是 展开
if (txtOpenOrClose.getText().equals(OPEN)) {
//修改为 收起
txtOpenOrClose.setText(CLOSE);
//隐藏短文字的文本框
shortTxt.setVisibility(View.GONE);
//显示长文字的文本框
longTxt.setVisibility(View.VISIBLE);
} else if (txtOpenOrClose.getText().equals(CLOSE)) {
txtOpenOrClose.setText(OPEN);
shortTxt.setVisibility(View.VISIBLE);
longTxt.setVisibility(View.GONE);
}
}
};

/**
* 视图绘制回调方法
*/
private OnPreDrawListener onPreDrawListener = new OnPreDrawListener() {

@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
//如果已经初始化布局,则不执行后面的方法
if(isInit){
return true;
}
//如果超过三行就显示 “展开” 的按钮
if (isShowAll(shortTxt, longTxt)) {
txtOpenOrClose.setVisibility(View.VISIBLE);
}

isInit=true;
return true;
}
};

/**
* 判断文字是否超过三行,返回布尔值
* @param shortTxt
* @param longTxt
* @return
*/
public boolean isShowAll(TextView shortTxt, TextView longTxt) {

int shortHeight = shortTxt.getHeight();
int longHeight = longTxt.getHeight();
if (longHeight > shortHeight) {
shortTxt.setVisibility(View.VISIBLE);
longTxt.setVisibility(View.GONE);
return true;
} else {
shortTxt.setVisibility(View.GONE);
longTxt.setVisibility(View.VISIBLE);
return false;
}

}


}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/id_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/id_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:maxLines="3"
android:textSize="16sp"
android:textColor="#000000" />

<TextView
android:id="@+id/id_textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#000000" />
</LinearLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >

<TextView
android:id="@+id/id_openOrClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="展开"
android:textColor="#0099FF" />
</RelativeLayout>

</LinearLayout>