小技巧:TextView 展开收起

来源:互联网 发布:php 编译mysql 编辑:程序博客网 时间:2024/06/04 20:05

直接上代码:

布局:        <TextView            android:id="@+id/tv_info"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/spacing_15dp"            android:layout_marginRight="@dimen/spacing_15dp"            android:layout_marginTop="@dimen/spacing_6dp"            android:ellipsize="end"            android:textColor="@color/btn_small_text"            android:textSize="12sp" />        <TextView            android:id="@+id/tv_more"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginRight="@dimen/spacing_8dp"            android:gravity="right"            android:padding="@dimen/spacing_7dp"            android:text="展开"            android:textColor="@color/main_green_color"            android:textSize="12sp"            android:visibility="gone" />
private void intView(){        tvMore.setText("展开");        tvInfo.setMaxLines(100000);//设置足够大行数;让文字全部展开        tvInfo.setText("要设置的文字");        int lineCount = tvInfo.getLineCount();        if (lineCount > 4/*最多显示行数,超过这个行数则显示展开按钮*/) {            tvInfo.setMaxLines(4);            tvMore.setVisibility(View.VISIBLE);        } else {            tvMore.setVisibility(View.GONE);        }}    @Override    public void onClick(View v) {        if (v == tvMore) {            if (tvMore.getText().equals("展开")) {                tvInfo.setMaxLines(100000);                tvMore.setText("收起");            } else {                tvInfo.setMaxLines(4);                tvMore.setText("展开");            }        }    }

哈哈, 就这么简单就实现 折叠功能了。

1 0