Android TextView 文本摺疊效果


Android TextView 文本摺疊效果

最近項目中要實現文本展開收起的效果,即默認只顯示4行文字,如果textview文字超過4行的話,點擊右下角的 更多 按鈕即可查看全部的內容。之前的做法是根據 TextView 中的字數來判斷,效果不太好。這裏在一個FrameLayout 包裹兩個 TextView


佈局文件 activity_main.xml

[html] view plaincopy
  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 plaincopy
  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



發佈了12 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章