本例是直接用TextView.setMaxLines(Integer.MAX_VALUE);
先看效果图
先看activity_main.xml
很简单的布局,可以整一个自己需要的布局
<?xml version="1.0" encoding="utf-8"?>
<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.example.huangqihua.expandtextview.MainActivity">
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF"
android:focusableInTouchMode="true"
android:gravity="center"
>
<TextView
android:id="@+id/expand_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="0dp"
android:paddingLeft="5dp"
android:paddingRight="30dp"
android:lineSpacingExtra="5dp"
android:maxLines="1"
android:text="Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"/>
</ScrollView>
<ImageView
android:id="@+id/expand_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="14dp"
android:layout_alignParentRight="true"
android:background="@drawable/class_feed_expand_image_tip_open"/>
</RelativeLayout>
再看MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView mExpandImage;
private TextView mExpandText;
private boolean bExpand; //
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandText = (TextView) findViewById(R.id.expand_text);
mExpandImage = (ImageView) findViewById(R.id.expand_image);
assert mExpandImage != null;
mExpandImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.expand_image:
if (!bExpand){
mExpandText.setMaxLines(Integer.MAX_VALUE);
}else {
mExpandText.setMaxLines(1);
}
doAnimate();
bExpand = !bExpand;
break;
}
}
private void doAnimate() {
ValueAnimator animator = null;
if (!bExpand) {
animator = ObjectAnimator.ofFloat(mExpandImage, "rotation", 0, 180);
} else {
animator = ObjectAnimator.ofFloat(mExpandImage, "rotation", 180, 0);
}
animator.setDuration(300);
animator.start();
}
}