編輯:關於 Android 程式設計
文字太長、空間太窄,又不想截斷——這時候走馬燈(marquee)是最直接的答案。ApiDemo 裡有 Button 的示範,但換成 TextView 時屬性組合略有不同,這裡把兩種都整理出來。
<TextView
android:layout_width="40px"
android:layout_height="wrap_content"
android:text="Test marquee for TextView"
android:layout_gravity="center"
android:ellipsize="marquee"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true" />
<Button
android:id="@+id/buttonMarquee"
android:layout_width="320px"
android:layout_height="wrap_content"
android:text="@string/textMarquee"
android:singleLine="true"
android:focusable="true"
android:selectAllOnFocus="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever" />
其中 @string/textMarquee 定義在 res/values/strings.xml。
marquee_forever 以外的整數十個有九個是因為「沒有取得焦點」。走馬燈只在該 View 處於 focused 或 selected 狀態時才會開始捲動。在觸控模式下,View 預設不可 focus,所以 focusableInTouchMode="true" 這一行是必要的;若用程式控制,可以呼叫 textView.requestFocus() 或 setSelected(true) 明確觸發。
另一個常見狀況是把 TextView 放進 ListView 的項目裡。此時焦點屬於整個項目列而非內部的 TextView,走馬燈不會啟動;解法是對 TextView 呼叫 setSelected(true),或改用 TextSwitcher、自訂 ValueAnimator 做位移動畫。
最後提醒,scrollHorizontally="true" 在 TextView 上建議一併加上,可避免文字被換行處理而讓 marquee 失效。至於 px 與 dp 的選用,範例沿用舊寫法用 px,實作時請改用 dp 以適配不同螢幕密度。
Android 插值器 Interpolator 全解:九種系統實現與自定義玩法
動畫的「手感」幾乎全由 Interpolator 決定。它是一個把 0~1 的時間進度映射成另一個數值的函數,映射曲線不同,位移、縮放、透明度看起來的加速節奏就完全不同。
OpenGL ES 2.0 系列(八):紋理貼圖——把圖片貼到三角形上
前面幾篇我們用 OpenGL ES 2.0 畫過各種規則形體,也介紹了 GLSL。但真實的 OpenGL 應用與遊戲,畫面裡從來不是只有方圓三角——是無數不規則的模型,每
ConstraintLayout 完全解析:告別多层嵌套的布局新思路
ConstraintLayout 是 Android Studio 2.2 的重點新增功能之一,也是 Google 在當年 I/O 大會上大力宣傳的能力。 在此之前,An
Android Studio 結合 GitHub 的入門流程
對 Android 開發者而言,版本控制幾乎是必備技能。GitHub 除了能保存程式碼,也方便協作與備份。本文整理兩種常見做法:一種是直接在 Android Studio