Android TextView 文本折叠效果

最近项目中要实现文本展开收起的效果,即默认只显示4行文字,如果textview文字超过4行的话,点击右下角的 更多 按钮即可查看全部的内容。之前的做法是根据 TextView 中的字数来判断,效果不太好。这里在一个FrameLayout 包裹两个 TextView


布局文件 activity_main.xml

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:padding="10dp"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tv_title"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="10dp"  
  14.         android:layout_marginBottom="10dp"  
  15.         android:text="@string/textview_fold" />  
  16.   
  17.     <FrameLayout  
  18.         android:id="@+id/fl_desc"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_below="@id/tv_title"  
  22.         android:fadingEdge="horizontal"  
  23.         android:fadingEdgeLength="5dp" >  
  24.   
  25.         <TextView  
  26.             android:id="@+id/tv_desc_short"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:maxLines="4"  
  30.             android:textColor="@color/black"  
  31.             android:textSize="16sp" />  
  32.   
  33.         <TextView  
  34.             android:id="@+id/tv_desc_long"  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:textColor="@color/black"  
  38.             android:textSize="16sp" />  
  39.     </FrameLayout>  
  40.   
  41.     <Button  
  42.         android:id="@+id/bt_more"  
  43.         android:layout_width="50dp"  
  44.         android:layout_height="25dp"  
  45.         android:layout_alignParentRight="true"  
  46.         android:layout_below="@id/fl_desc"  
  47.         android:layout_marginRight="10dp"  
  48.         android:background="#1c000000"  
  49.         android:gravity="center"  
  50.         android:text="@string/label_more"  
  51.         android:textSize="15sp"  
  52.         android:visibility="gone" />  
  53.   
  54.     <ImageView  
  55.         android:id="@+id/iv_more_line"  
  56.         android:layout_width="fill_parent"  
  57.         android:layout_height="wrap_content"  
  58.         android:layout_alignBaseline="@id/bt_more"  
  59.         android:layout_below="@id/fl_desc"  
  60.         android:layout_toLeftOf="@id/bt_more"  
  61.         android:background="@drawable/more_line"  
  62.         android:contentDescription="@string/app_name"  
  63.         android:visibility="gone" />  
  64.   
  65. </RelativeLayout>  


MainActivity.java

[java]  view plain copy
  1. package com.example.textviewfold;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.ViewTreeObserver;  
  8. import android.view.ViewTreeObserver.OnPreDrawListener;  
  9. import android.widget.Button;  
  10. import android.widget.FrameLayout;  
  11. import android.widget.ImageView;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends Activity implements OnClickListener {  
  15.   
  16.     private Button bt_more;  
  17.     private FrameLayout fl_desc;  
  18.     private TextView tv_desc_short;  
  19.     private TextView tv_desc_long;  
  20.     private boolean isInit = false;  
  21.     private boolean isShowShortText = true;  
  22.     private ImageView iv_more_line;  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.   
  29.         findView();  
  30.         initView();  
  31.         setListener();  
  32.     }  
  33.   
  34.     private void setListener() {  
  35.         bt_more.setOnClickListener(this);  
  36.     }  
  37.   
  38.     private void initView() {  
  39.         String content = "新浪科技讯 北京时间7月25日凌晨消息,在今天举行的新产品发布会上,谷歌发布Android 4.3版本,代号仍为\"果冻豆(Jelly Bean)\"。今天发布的新一代Nexus 7将搭载该操作系统,Nexus系列设备今日可收到OTA推送更新。\r\nAndroid 4.3操作系统新增一系列功能。首先是多用户设置功能,包括针对保护儿童的“受限文件(restricted profiles)”特性。用户可以对应用内容进行限制,防止儿童在使用应用时看到不适宜内容,或接触不合适的应用内购买广告。这项功能与微软Windows Phone的\"儿童乐园(Microsoft's Kid's Corner)\"功能类似。\r\n第二项升级是智能蓝牙(Bluetooth Smart)功能,即\"低功耗蓝牙(Bluetooth Low Energy)\"。";  
  40.         tv_desc_short.setText(content);  
  41.         tv_desc_long.setText(content);  
  42.   
  43.         ViewTreeObserver vto = fl_desc.getViewTreeObserver();  
  44.         vto.addOnPreDrawListener(new OnPreDrawListener() {  
  45.             @Override  
  46.             public boolean onPreDraw() {  
  47.                 if (isInit)  
  48.                     return true;  
  49.                 if (mesureDescription(tv_desc_short, tv_desc_long)) {  
  50.                     iv_more_line.setVisibility(View.VISIBLE);  
  51.                     bt_more.setVisibility(View.VISIBLE);  
  52.                 }  
  53.                 isInit = true;  
  54.                 return true;  
  55.             }  
  56.         });  
  57.     }  
  58.   
  59.     /** 
  60.      * 计算描述信息是否过长 
  61.      */  
  62.     private boolean mesureDescription(TextView shortView, TextView longView) {  
  63.         final int shortHeight = shortView.getHeight();  
  64.         final int longHeight = longView.getHeight();  
  65.         if (longHeight > shortHeight) {  
  66.             shortView.setVisibility(View.VISIBLE);  
  67.             longView.setVisibility(View.GONE);  
  68.             return true;  
  69.         }  
  70.         shortView.setVisibility(View.GONE);  
  71.         longView.setVisibility(View.VISIBLE);  
  72.         return false;  
  73.     }  
  74.   
  75.     private void findView() {  
  76.         fl_desc = (FrameLayout) findViewById(R.id.fl_desc);  
  77.         tv_desc_short = (TextView) findViewById(R.id.tv_desc_short);  
  78.         tv_desc_long = (TextView) findViewById(R.id.tv_desc_long);  
  79.   
  80.         bt_more = (Button) findViewById(R.id.bt_more);  
  81.         iv_more_line = (ImageView) findViewById(R.id.iv_more_line);  
  82.     }  
  83.   
  84.     @Override  
  85.     public void onClick(View v) {  
  86.         switch (v.getId()) {  
  87.         case R.id.bt_more:  
  88.             if (isShowShortText) {  
  89.   
  90.                 tv_desc_short.setVisibility(View.GONE);  
  91.                 tv_desc_long.setVisibility(View.VISIBLE);  
  92.             } else {  
  93.                 tv_desc_short.setVisibility(View.VISIBLE);  
  94.                 tv_desc_long.setVisibility(View.GONE);  
  95.             }  
  96.             toogleMoreButton(bt_more);  
  97.             isShowShortText = !isShowShortText;  
  98.             break;  
  99.   
  100.         default:  
  101.             break;  
  102.         }  
  103.     }  
  104.   
  105.     /** 
  106.      * 更改按钮【更多】的文本 
  107.      */  
  108.     private void toogleMoreButton(Button btn) {  
  109.   
  110.         String text = (String) btn.getText();  
  111.         String moreText = getString(R.string.label_more);  
  112.         String lessText = getString(R.string.label_less);  
  113.         if (moreText.equals(text)) {  
  114.             btn.setText(lessText);  
  115.         } else {  
  116.             btn.setText(moreText);  
  117.         }  
  118.     }  
  119. }  



运行效果

为展开的状态



展开后的状态



OK,到这就完了,有更好的实现方法,可以一起讨论哦!



工程下载地址:http://download.csdn.net/detail/fx_sky/5812437


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值