直接前往內容

最常造訪的網頁

最近瀏覽的網頁

navigation

Creating Custom Layouts for Wear

With Wear OS by Google, creating layouts for a watch is similar to creating them for a phone, except that you have to design for the screen size and for glanceability. Do not port the functionality and UI from a phone app to a watch and expect a good experience.

You should create custom layouts only when necessary. Read the Designing for Wear OS guidelines for information on designing great watch apps.

Create Custom Notifications

In general, you should create notifications on the phone and let them automatically sync to the wearable. This lets you build your notifications once and have them appear on many types of devices (not just watches, but eventually Auto and TV) without having to design them for different form factors.

If the standard notification styles don't work for you (such as NotificationCompat.BigTextStyle or NotificationCompat.InboxStyle), you can display an activity with a custom layout. You can only create and issue custom notifications on the watch, and the system does not sync these notifications to the phone.

Note: When creating custom notifications on the watch, you can use the standard notification APIs (API Level 20) instead of the Support Library.

To create a custom notification:

  1. Create a layout and set it as the content view for the activity that you want to display.
    public void onCreate(Bundle bundle){
        ...
        setContentView(R.layout.notification_activity);
    }
    
  2. Define necessary properties for the activity in the Android manifest to allow the activity to be displayed in the watch's context stream process. You need to declare the activity to be exportable, be embeddable, and have an empty task affinity. We also recommend setting the theme to Theme.DeviceDefault.Light. For example:
    <activity android:name="com.example.MyDisplayActivity"
        android:exported="true"
        android:allowEmbedded="true"
        android:taskAffinity=""
        android:theme="@android:style/Theme.DeviceDefault.Light" />
    
  3. Create a PendingIntent for the activity that you want to display. For example:
    Intent notificationIntent = new Intent(this, NotificationActivity.class);
    PendingIntent notificationPendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
  4. Build a Notification and call setDisplayIntent() providing the PendingIntent. The system uses this PendingIntent to launch the activity when users view your notification.
  5. Issue the notification using the notify() method.

    Note:In Wear 1.x, when a notification is peeking on the homescreen, the system displays it with a standard template that it generates from the notification's semantic data. This template works well on all watchfaces. When users swipe the notification up, they'll then see the custom activity for the notification.

Create Layouts with the Wear UI Library

The Wear UI Library is automatically included when you create your watch app with the Android Studio Project Wizard. You can also add this library to your build.gradle file with the following dependency declaration:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:wear:26.0.0'
    compile 'com.google.android.gms:play-services-wearable:+'
}

This library helps you build UIs that are designed for watches. For more information, see Creating Custom UIs for Wear Devices.

Here are some of the major classes:

BoxInsetLayout
A FrameLayout object that's aware of screen shape and can box its children in the center square of a round screen.
ConfirmationActivity
An activity that displays confirmation animations after the user completes an action.
AnimationSet
A group of animations that should be played together.
CircularProgressLayout
A layout that provides a circular countdown timer around a child view. Typically used as an automatic timer to confirm an operation after a short delay has elapsed.
SnapHelper
SnapHelper supports snapping for a RecyclerView object.
PagerSnapHelper
Implementation of the SnapHelper instance supporting pager style snapping in either vertical or horizontal orientation.
AlertDialog
A subclass of Dialog that can display one, two or three buttons.
ProgressBar
Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward.
WearableRecyclerView
Wearable specific implementation of the RecyclerView class for displaying scrollable lists of items in square and round devices.

Wear UI library API reference

The reference documentation explains how to use each UI widget in detail. Browse the Wear API reference documentation for the classes above.

Note: We recommend using Android Studio for Wear OS development, as it provides project setup, library inclusion, and packaging conveniences.

本網站使用 Cookie 儲存你在此指定的語言和顯示選項偏好設定。

掌握有關 Android 開發人員的最新消息和實用訣竅,協助你製作最受歡迎的 Google Play 內容。

* 必填欄位

訂閱成功!

在 WeChat 上追蹤 Google Developers

你要以瀏覽這個網站嗎?

你要求以顯示這個網頁,但你為此網站指定的語言偏好設定為

是否要變更語言偏好設定並改用瀏覽網站?稍後如要變更語言偏好設定,請利用位在每個網頁最下方的語言選單來調整設定。

你的 API 層級必須達 以上才能存取這個級別

本說明文件已隱藏,因為你為該文件選取的 API 層級為 。使用左側導覽列上方的選取工具即可變更說明文件的 API 層級。

如需進一步瞭解如何為應用程式指定 API 層級的相關資訊,請參閱 Supporting Different Platform Versions (支援不同的平台版本) (英文)。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)