<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" > <!-- android 简单的两步实现Play上的文本伸缩功能 -->
<!-- http://www.cnblogs.com/TerryBlog/archive/2013/03/12/2956165.html -->
<FrameLayout
android:id="@+id/expandable_container"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="@dimen/linear_margin_bigger" > <TextView
android:id="@+id/expandable_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:ellipsize="end"
android:text="@string/loremipsum" />
</FrameLayout> <!-- my method -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <TextView
android:id="@+id/myexpandable_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="3"
android:clickable="true"
android:ellipsize="end"
android:text="@string/loremipsum" />
</FrameLayout> </LinearLayout>
package com.lxh.textview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView; import com.example.com.lxh.pro1.R; /**
* android 简单的两步实现Play上的文本伸缩功能
* http://www.cnblogs.com/TerryBlog/archive/2013/03/12/2956165.html
* @author lxh
*/
public class ExpandableTV extends Activity {
// -------------------------- Terry's method --------------------------
private boolean isExpanded = false, myIsExpanded = false;
private int lastHeight = 0;
private ViewGroup expandableContainer;
private TextView textView, myTextView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_tv);
expandableContainer = (ViewGroup) findViewById(R.id.expandable_container);
textView = (TextView) findViewById(R.id.expandable_text);
textView.setOnClickListener(terryClickListener);
myTextView = (TextView) findViewById(R.id.myexpandable_text);
myTextView.setOnClickListener(myClickListener);
} private OnClickListener terryClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (isExpanded) {
LayoutParams params = (LayoutParams) expandableContainer.getLayoutParams();
params.height = lastHeight;
expandableContainer.setLayoutParams(params);
expandableContainer.invalidate(); //调用OnDraw方法
} else {
lastHeight = expandableContainer.getHeight();
LayoutParams params = (LayoutParams) expandableContainer.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
expandableContainer.setLayoutParams(params);
expandableContainer.invalidate(); //调用OnDraw方法
}
isExpanded = !isExpanded;
}
};
//Terry总结:此方法缺陷,当给出最大高度不合适时,字会显示一半高度 // -------------------------- my method --------------------------
private OnClickListener myClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (myIsExpanded) {
myTextView.setMaxLines(3);
expandableContainer.invalidate(); //调用OnDraw方法
} else {
myTextView.setMaxLines(Integer.MAX_VALUE);
expandableContainer.invalidate(); //调用OnDraw方法
}
myIsExpanded = !myIsExpanded;
}
};
//Me总结:此方法不存在Terry的问题
}
05-22 04:02