Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自製可收合/展開的 TextView:PhilExpandableTextView 原理與完整原始碼

編輯:關於 Android 開發

為什麼需要可收合的 TextView

你一定見過這樣的設計:應用市場裡某個 App 的介紹文字洋洋灑灑數百字,但畫面上只顯示三、四行摘要,底下附一個展開按鈕,有興趣的人點開來才看全文。

這個模式之所以普遍,是因為它尊重使用者的注意力。使用者在手機上的時間有限、專注度也有限,比起一次丟出一大段文字,先給出重點再讓使用者決定要不要深入,顯然是更體貼的做法。

本文示範如何在原生 TextView 的基礎上,實作一個可收合/展開的元件:PhilExpandableTextView。

實作原理

核心其實只有一件事:控制 TextView 的 maxLines。

在 TextView 完成初始化、但尚未真正繪製之前,透過 ViewTreeObserver 監聽 onPreDraw 事件,取得「完整顯示這段文字需要幾行」,接著把最大行數限制在一個較小的值(例如 3 行),畫面上就呈現出收合的摘要效果。使用者點擊展開時,再把最大行數放寬到實際行數再加一(多留一行餘量,避免估算不足而截斷最後一句)。

圖 1 是收合狀態:

圖 2 是點擊按鈕展開後的樣子:

測試用的 MainActivity.java

 1 package zhangphil.textview; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 8 public class MainActivity extends Activity { 9 10 private String test_str = ""; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main); 16 17 // 測試的字符串 18 for (int i = 0; i < 100; i++) 19 test_str = test_str + " " + i; 20 21 final PhilExpandableTextView text = (PhilExpandableTextView) findViewById(R.id.text); 22 text.setText(test_str); 23 24 Button button = (Button) findViewById(R.id.button); 25 button.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 boolean b=text.getExpandableStatus(); 29 b=!b; 30 text.setExpandable(b); 31 } 32 }); 33 } 34 }

這裡刻意用迴圈組出一段長文字,方便觀察收合效果。按鈕的邏輯是「讀取目前狀態 → 反轉 → 套用」,因此同一顆按鈕可以同時負責展開與收合。

版面配置 activity_main.xml

 1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="zhangphil.textview.MainActivity" > 6 7 <zhangphil.textview.PhilExpandableTextView 8 android:id="@+id/text" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_alignParentTop="true" 12 android:background="#03a9f4" /> 13 14 <Button 15 android:id="@+id/button" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_alignParentBottom="true" 19 android:layout_centerHorizontal="true" 20 android:text="Button" /> 21 22 </RelativeLayout>

核心元件 PhilExpandableTextView.java

 1 package zhangphil.textview; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.view.ViewTreeObserver; 6 import android.widget.TextView; 7 8 public class PhilExpandableTextView extends TextView { 9 10 // 最大的行,默認只顯示3行 11 private final int MAX = 3; 12 13 // 如果完全伸展需要多少行? 14 private int lines; 15 16 private PhilExpandableTextView mPhilTextView; 17 18 // 標記當前TextView的展開/收縮狀態 19 // true,已經展開 20 // false,以及收縮 21 private boolean expandableStatus = false; 22 23 public PhilExpandableTextView(Context context, AttributeSet attrs) { 24 super(context, attrs); 25 26 mPhilTextView = this; 27 28 init(); 29 } 30 31 private void init() { 32 // ViewTreeObserver View觀察者,在View即將繪制但還未繪制的時候執行的,在onDraw之前 33 final ViewTreeObserver mViewTreeObserver = this.getViewTreeObserver(); 34 35 mViewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 36 @Override 37 public boolean onPreDraw() { 38 // 避免重復監聽 39 mPhilTextView.getViewTreeObserver().removeOnPreDrawListener(this); 40 41 lines = getLineCount(); 42 // Log.d(this.getClass().getName(), lines+""); 43 44 return true; 45 } 46 }); 47 48 setExpandable(false); 49 50 //setEllipsize(TextUtils.TruncateAt.END); 51 } 52 53 // 是否展開或者收縮, 54 // true,展開; 55 // false,不展開 56 public void setExpandable(boolean isExpand) { 57 if (isExpand) { 58 setMaxLines(lines + 1); 59 } else 60 setMaxLines(MAX); 61 62 expandableStatus = isExpand; 63 } 64 65 public boolean getExpandableStatus() { 66 return expandableStatus; 67 } 68 }

幾個值得注意的實作細節

為什麼要用 onPreDraw:在 onDraw() 之前,文字已經完成量測,getLineCount() 才拿得到正確行數;而畫面還沒畫上去,此時調整 maxLines 不會造成閃爍。

一定要記得移除監聽:OnPreDrawListener 每次繪製都會觸發,若不呼叫 removeOnPreDrawListener(this),就會無限循環。範例中把 this 移除寫在回呼第一行,是標準做法。

+1 的用意:行數計算在某些字型或行距下可能少算一行,多給一行餘量能避免最後一個字被硬切掉。

可再加強的地方:實務上通常還會加上「展開/收合」的文字提示或箭頭圖示,並在收合狀態用 setEllipsize(TextUtils.TruncateAt.END) 讓最後一行出現省略號,視覺訊號會清楚很多。

本文轉載自:https://blog.csdn.net/zhangphil/article/details/50088465

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved