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

Android AnalogClock 類比時鐘元件基本用法

編輯:關於 Android 程式設計

本文示範 Android 內建的 AnalogClock(指針式時鐘)元件如何使用,結論同樣很輕鬆:寫在版面裡就能動。

版面即邏輯

AnalogClock 會自行讀取系統時間並驅動時針、分針與秒針,因此不需要在 Activity 中 new 出物件,也不用掛任何計時器。把標籤放在想顯示的位置,並用 layout_gravity 決定對齊方式即可。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <AnalogClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

想換錶盤怎麼辦

內建的 AnalogClock 只認 dial 與 hand_hour、hand_minute 這幾個內部資源,官方並未開放替換。若專案需要品牌化的錶盤,實務上會直接自訂 View,用 Canvas 畫出刻度與指針,再依時間換算旋轉角度,這樣自由度遠高於沿用預設元件。

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