This lesson teaches you to
You should also read
Creating layouts for wearables is the same as handheld devices, except you have to design for the screen size and for glanceability. Do not port functionality and the UI from a handheld app and expect a good experience. You should create custom layouts only when necessary. Read the design guidelines for information on how to design great wearable apps.
创建手表的布局和手机设备一样,除了你需要设计屏幕的尺寸和平衡。不要直接将手机的应用的功能和UI 直接用在手表上面,这样不会有一个好的体验的。你应该只有在需要的时候才创建自定义的布局。阅读设计的向导去得到更多的关于如何设计好的手表应用。
Create Custom Notifications 创建自定义的通知。
In general, you should create notifications on the handheld 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 wearables, 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
orNotificationCompat.InboxStyle
), you can display an activity with a custom layout. You can only create and issue custom notifications on the wearable, and the system does not sync these notifications to the handheld.
如果标准的通知样式不符合你的要求,那么你可以使用自定义布局来展示你的actiivty。你可以只在手表上面创建和启动自定义的布局,并且系统不会同步这些通知到手机端。
Note: When creating custom notifications on the wearable, you can use the standard notification APIs (API Level 20) instead of the Support Library.
To create a custom notification:
注意,当创建手表上面的自定义的通知的时候,你可以使用标准的通知API 而不是支持库。
为了创建一个自定义的通知:
1:创建一个布局并且在activity 中设置
- 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); }
- Define necessary properties for the activity in the Android manifest to allow the activity to be displayed in the wearable'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 去展示手表的上下文的流进程。你需要这个actiivty 为exporteable,emabeddable,并且有一个空的任务affinity。我们还建议设置主题为Theme.DeviceDefault.Light
.<activity android:name="com.example.MyDisplayActivity" android:exported="true" android:allowEmbedded="true" android:taskAffinity="" android:theme="@android:style/Theme.DeviceDefault.Light" />
- Create a
PendingIntent
for the activity that you want to display. For example: 创建一个PendingIntent 给你需要显示的activity,例如Intent notificationIntent = new Intent(this, NotificationActivity.class); PendingIntent notificationPendingIntent = PendingIntent.getActivity( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- Build a
Notification
and callsetDisplayIntent()
providing thePendingIntent
. The system uses thisPendingIntent
to launch the activity when users view your notification.创建一个通知并且调用setDisplayIntent() 去提供PendingIntent。系统使用这个PendingIntent去启动这个activity当用户查看你的通知的时候。 - Issue the notification using the
notify()
method. 通过notify()方法来启动通知。Note: When the 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. 当通知在主屏幕上面显示,系统使用标准的模版来展示语义数据。这个模版在手表表盘上面运行的很好。当用户将通知向上滑动的时候,他们看到自定义的通知的activity。
Create Layouts with the Wearable UI Library 创建含有手表UI库的布局。
The Wearable UI Library is automatically included when you create your wearable app with the Android Studio Project Wizard. You can also add this library to your build.gradle
file with the following dependency declaration:在你使用Android Studio 项目向导来创建手表应用的时候,手表UI库是自动的包含进来的。你也可以将这个库添加到你的build.gradle 文件中使用下面的依赖声明。
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.support:wearable:+' compile 'com.google.android.gms:play-services-wearable:+' }
This library helps you build UIs that are designed for wearables. For more information, see Creating Custom UIs for Wear Devices.
这个库帮助你创建UIs
Here are some of the major classes in the Wearable UI Library:下面是手表UI库中的主要的类
-
A
FrameLayout
object that's aware of screen shape and can box its children in the center square of a round screen. - 一个框架布局对象,他可以感知到屏幕的尺寸并且将他的子控件放在圆形屏幕的中见框中。
- A fragment that presents content within an expandable, vertically scrollable card.
- 一个fragment 用来展示内容在一个可以扩展的,垂直滑动的卡片。
- An image view surrounded by a circle.
- 一个图片视图走位包含了一个圆形
- An activity that displays confirmation animations after the user completes an action.
- 一个activity用来展示在用户完成了某个action 的时候确认的动画
- A drawable that contains two child drawables and provides methods to directly adjust the blend between the two.
- 一个drawable 包含了两个子的drawable 并且提供了放去去直接适应两个的混淆。
- A view that provides a circular countdown timer, typically used to automatically confirm an operation after a short delay has elapsed.
- 一个视图提供了圆形的倒计时,一般是用来在一个短的迟疑的事件消失(elapse)后自动的确认一个操作的
-
A view for implementing long-press-to-dismiss.
为了实现长按消除的功能的视图 -
A layout manager that allows the user to navigate both vertically and horizontally through pages of data. You supply an implementation of a
GridPagerAdapter
instance to generate the pages that the view shows. - 一个布局管理器,它允许用户去垂直的和水平的在页面数据上面定位。你可以实现一个GridPagerAdapter 的实例来产生视图显示的页面
-
An adapter that supplies pages to a
GridViewPager
object. - 为GridViewPager 对象提供页面的
-
An implementation of a
GridPagerAdapter
instance that represents each page as a fragment. - 一个实现了GridPagerAdapter实例展示的每一个页面都是一个fragment。
-
A page indicator for a
GridViewPager
implementation that identifies the current page in relation to all available pages on the current row. - 一个GridViewPager 实现的页面定位,它确定了当前的页面,这个页面和所有当前可用个页面
- A class that can inflate a specific layout, depending on the shape of the device's screen.
- 一个类,这个类可以 依赖设备的尺寸填充特殊的布局。
-
An alternative version of a
ListView
object that is optimized for ease of use on small screen wearable devices. It displays a vertically scrollable list of items, and automatically snaps to the nearest item when the user stops scrolling. - 一个ListView 对象的改变的版本,这个已经为了在小手表的屏幕上面方便使用进行了优化。它展示了一个垂直的可滑动的list项目,并且当用户停止滑动的时候自动的选择最近的项目item。
BoxInsetLayout
CardFragment
CircledImageView
ConfirmationActivity
CrossFadeDrawable
DelayedConfirmationView
DismissOverlayView
GridViewPager
GridPagerAdapter
FragmentGridPagerAdapter
DotsPageIndicator
WatchViewStub
WearableListView
Wear UI library API reference 手表UI库的参考
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 Android Wear development, as it provides project setup, library inclusion, and packaging conveniences.