編輯:關於 Android 程式設計
微信朋友圈有個很實用的細節:好友發表的文字太長時會自動收縮,底部出現「顯示更多」提示,點擊後才展開全文,再點一次變成「收起」。這個效果的實作原理比想像中簡單——核心就是控制 TextView 的最大行數。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
兩個 TextView 分工明確:desc_tv 顯示本文內容,desc_op_tv 是「顯示更多/收起」的觸發文字,預設隱藏。
<string name="desc_shrinkup">收起</string> <string name="desc_spread">顯示更多</string>
流程分三步:
微信的預設最大行數是 10 行,這裡設為 2 行以便觀察效果。這個值由常量 DEFAULT_MAX_LINE_COUNT 控制,可自行調整。
public class CollapsibleTextView extends LinearLayout implements OnClickListener {
/** 預設顯示的最大行數 */
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;
}
}
}
}
因為只有在配置(layout)完成後,getLineCount() 才有正確的值。在構造函數裡呼叫會永遠得到 0,判斷必然出錯。
onLayout 可能被呼叫多次(每次 requestLayout 後都會觸發)。flag 的作用是讓「判斷行數並設定初始狀態」這件事只做一次,後續的點擊則由 onClick 把 flag 重設為 false 來重新觸發。
InnerRunnable 透過 post 排到下一輪訊息迴圈,是為了避開「在 onLayout 執行期間又呼叫 setMaxLines 觸發重新配置」所造成的遞迴。這是 Android View 體系裡常見的迴避手法。
這個實作把「行數判斷 + 展開收合 + 提示文字切換」都揉在同一個類別裡,功能完整但有幾個可以改進的點:
ValueAnimator 搭配動態改變高度,或在 API 21+ 使用 TransitionManager.beginDelayedTransition() 達到自動過渡動畫。attrs.xml + obtainStyledAttributes)讓版面檔能直接指定,而不是改原始碼重新編譯。TouchDelegate 擴大觸控範圍,或讓整塊 bottom 區域都可點。setDesc 之後若文字被替換成更短的內容,收合狀態不會重算。應該在 setText 後重設 flag 並重新 requestLayout。TextView 的 Ellipsize。API 16 以後 setMaxLines 搭配 Ellipsize.END 可以在末行自動顯示省略號,效果更接近原生體驗。整體來說,這個範例最值得學習的是「何時才能安全地取得 View 的量測結果」這個觀念。getLineCount()、getMeasuredWidth()、getHeight() 這類值都必須在 layout 完成後才有效,理解這一點能避免大量「數值永遠是 0」的疑难。
Android 網絡編程(十):Retrofit2 請求參數注解全解
前言 上一篇我們掌握了 Retrofit 最基礎的 GET 請求寫法,並對請求參數有了初步認識。本篇會把 Retrofit2 提供的各類請求參數注解系統梳理一遍,包括路徑
小米手機助手怎麼刷機?用電腦端完成系統升級的步驟
手機賣得好,順手做一個手機助手,說是工具不如說是給使用者的福利。vivo 有 vivo 手機助手,小米則有小助手。它能做的事不少,其中最多人問的就是:能不能用它刷機?答案
Android 平鋪圖片實作教學:XML 與程式碼兩種寫法
做 App 時常會遇到這種狀況:設計師丟來一張帶有鋸齒邊緣的背景素材,卻只給了「一格」的樣本,剩下的要你自己想辦法鋪滿整個區域。本文就以這個鋸齒邊框為例,說明在 Andr
自定義 View 實戰:用像素判斷做出電視遙控式環形按鈕(上)
這篇的目標很明確:用最樸素的方式,做出下圖這種環形排列的按鈕效果,並且把每一步都講清楚。 老規矩,先看效果: 這是一個點餐界面,六種菜品意味著六個按鈕。點擊「開始點餐」