Create a custom layout (Creating custom Layouts) This article describes how to create a custom notification and use the wearable UI library to create a custom layout
You also need to know
Wearable Design Guidelines(Wear Design Principles)
In addition to screen size and instantaneous read capability (Glance ability), creating a layout for wearable apps is roughly the same as a regular phone.
Create a custom notification (named notifications)Typically, you should create a notification on your phone app and then automatically sync to your wearable app. This allows you to build notifications only once,
Can be presented in a variety of devices (not just wearables, but ultimately cars and TVs) without having to design different device parameters separately.
If the standard notification style does not apply to you (such as Notificationcompat.bigtextstyle or Notificationcompat.inboxstyle),
You can use a custom layout activity to display notifications. You can only create and send custom notifications on wearables, and the system does not sync these notifications with your phone.
Note: When creating a custom notification on a wearable device, you can use standard notification APIs (API level 20) instead of the support library.
1. Create a layout and set the content view as the foreground activity
public void OnCreate (bundle bundle) { ... Setcontentview (r.layout.notification_activity);}
2. Define the necessary properties of the activity in the Android manifest file so that the activity can be rendered during the context flow of the wearable application.
You need to declare that this activity is output (exportable), embeddable (embeddable), and that task sharing (tasks affinity) is empty.
We also recommend setting the scheme (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 this activity
Intent notificationintent = new Intent (this, notificationactivity.class); Pendingintent notificationpendingintent = pendingintent.getactivity (this, 0, notificationintent,pendingintent.flag_ Update_current);
4. Build a notification (Notification) and call the Setdisplayintent () method to provide this pendingintent. When the user reads the notification,
The system uses this pendingintent to initiate the corresponding activity.
5. Use the notify () method to send notifications.
Note: When a notification appears on the home screen, the system uses a standard template that is generated from the notification semantics data to display it. This template is performing well on all the watch interfaces.
When the user slides up the notification, they will see the custom activity interface for that notification.
Use the Wearable UI library (wearable UI libraries) to create layoutsWhen you create a wearable app, an unofficial UI library is automatically included through the Android Studio Engineering Wizard.
You can add the following statement to the project build file 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 can be used to easily build wearable application interfaces. Here are some of the main classes:
- Boxinsetlayout-a framelayout that can perceive the shape of a screen and make its child elements appear in the display;
- Cardfragment-an extensible, vertical scrolling card-like interface component;
- Circledimageview-round picture view;
- Confirmationactivity-When the user operation is complete, confirm the information animation activity interface.
- Dismissoverlayview-a long press of the Vanishing (Long-press-to-dismiss) view;
- Gridviewpager-A data paging (can be horizontal, vertical) Display layout manager, you implement a gridpageradapter next page data to this view to display;
- Gridpageradapter-As mentioned above, this is a paging data generator;
- A derived implementation of fragmentgridpageradapter-gridpageradapter, used to treat each page as a fragment;
- Watchviewstub-A class that can populate a specific layout, depending on the screen shape of the device;
- An alternative version of Wearablelistview-listview, optimized for small-screen wearable devices. It displays a vertical scrolling list item that automatically snaps to the nearest list item when the user stops scrolling.
Please download the full API reference documentation for the wearable application programming support library with complete instructions for each interface component.
by Iefreer