几句代码搞定ExpandableTextView

本文介绍了一个简单的Android应用案例,通过点击按钮实现TextView显示文本行数的动态切换效果,包括源代码配置步骤及实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例是直接用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();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值