Android编程之仿微信显示更多文字的View

2019-03-02 00:48|来源: 网路

微信朋友圈中,如果好友发表的文字过长,会自动收缩起来,底下有提示,当点击“显示更多”时才会展开。

 

首先定义布局文件(很简单,不解释):

<?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" >

    <TextView
        android:id="@+id/desc_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4.0dip"
        android:gravity="center_vertical"
        android:textColor="#ff000000"
        android:textSize="14.0dip" />

    <TextView
        android:id="@+id/desc_op_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4.0dip"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="#ff576b95"
        android:textSize="14.0dip"
        android:visibility="gone" />

</LinearLayout>

 

文字:

    <string name="desc_shrinkup">收起</string>
    <string name="desc_spread">显示更多</string>


 

接下来就是对文字的处理,原理就是设置TextView最大行数。

先判断文字是否超过了规定的行数,如果没超过,不显现“显示更多”的文字提示。当文字超过了,就显示“显示更多”的文字提示。用户点击“显示更多”后,设置最大行数为最大。

补一句:微信的默认最大行数是10行,我这里设置是2行。你可以自行设置该值DEFAULT_MAX_LINE_COUNT。

 

代码如下:

public class CollapsibleTextView extends LinearLayout implements
        OnClickListener {

    /** default text show max lines */
    private static final int DEFAULT_MAX_LINE_COUNT = 2;

    private static final int COLLAPSIBLE_STATE_NONE = 0;
    private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
    private static final int COLLAPSIBLE_STATE_SPREAD = 2;

    private TextView desc;
    private TextView descOp;

    private String shrinkup;
    private String spread;
    private int mState;
    private boolean flag;

    public CollapsibleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        shrinkup = context.getString(R.string.desc_shrinkup);
        spread = context.getString(R.string.desc_spread);
        View view = inflate(context, R.layout.collapsible_textview, this);
        view.setPadding(0, -1, 0, 0);
        desc = (TextView) view.findViewById(R.id.desc_tv);
        descOp = (TextView) view.findViewById(R.id.desc_op_tv);
        descOp.setOnClickListener(this);
    }

    public CollapsibleTextView(Context context) {
        this(context, null);
    }

    public final void setDesc(CharSequence charSequence, BufferType bufferType) {
        desc.setText(charSequence, bufferType);
        mState = COLLAPSIBLE_STATE_SPREAD;
        requestLayout();
    }

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            if (desc.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
                mState = COLLAPSIBLE_STATE_NONE;
                descOp.setVisibility(View.GONE);
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
            } else {
                post(new InnerRunnable());
            }
        }
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            if (mState == COLLAPSIBLE_STATE_SPREAD) {
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(spread);
                mState = COLLAPSIBLE_STATE_SHRINKUP;
            } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
                desc.setMaxLines(Integer.MAX_VALUE);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(shrinkup);
                mState = COLLAPSIBLE_STATE_SPREAD;
            }
        }
    }
}


 

效果图:

 

 

 

 

代码工程:

http://download.csdn.net/detail/xyz_fly/5625783

 


转自:http://www.cnblogs.com/xinyuyuanm/p/3165504

相关问答

更多
  • 需要学习html,css,mysql,php或者java,还要学微信开发,韩顺平的149视频课程讲的是前面几个,泰牛程序员的微信公开课讲的就是微信开发视频的
  • 微信2下载[2024-02-01]

    您好,打开手机浏览器或者是应用商城,然后输入微信进行搜索,在搜索结果中下载安装这款软件即可
  • 主要是在交货前充分做好程序测试,是否能满足你的需求。 另外,售后服务一定要有保证,如果有更新协议的话最好确定更新时间和周期; 基本上就这样,最好选择一些信誉比较好的公司吧。企业开发微信小程序要明确自己的需求。 把需求提交给产品进行设计。 设计和开发做好小程序后提交审核最终上线。
  • 1:登陆手机微信客户端。 2:进入微信界面,在界面右下角有一个"我"字,点击进入。 3:进入后会看到”相册“”收藏“”钱包“等信息,点击进入”钱包“。 4:进入后会看到另一个界面,存在”零钱“”转账“”银行卡“等信息。
  • 用你上网的IP地址
  • 开通微信商城的流程如下: 一、注册微信公众平台的服务号; 我们必须先注册微信公众平台的服务号,因为只有服务号才可能开通支付功能,实现线上的交易。至于如何申请微信公平台请参照我们的《微信公众号的申请和完善》。 二、微信认证服务号(申请公函); 进入微信公众平台之后点击右侧中设置的微信认证,如何搭建自己的微商城(上) 点击之后就会出现微信认证的界面 如何搭建自己的微商城(上) 微信认证 根据提示进行一步一步的认证,我们的公众号认证可以算是基本成型,不过认证的最后是要给腾迅缴纳300元认证费用的。认证通过之后,服 ...
  • 不是仿,是直接登录到微信公众平台,但是没有通过他们的登录界面登录进去,而是用程序提交用户名密码登录进去,然后进行相关操作,往相关接口提交数据
  • 小程序本身是微信提供的一个平台,使用和注册都是不需要钱的(企业需要300认证费)。注册下来的名字也永久是你的,就是小程序你要用起来里面的功能是需要开发的,要么自己开发要么找第三方合作。