android wear那些事--创建自定义布局

来源:互联网 发布:js window.open php 编辑:程序博客网 时间:2024/05/21 14:04

Creating Custom Layouts

为穿戴式设备创建布局和在手机上的操作一样,不过为了整体平衡和谐感你需要根据屏幕尺寸进行设计.直接使用手机上的UI,用户体验不是很好.阅读设计指导获取如何设计优秀穿戴式app的信息.

创建自定义通知


一般情况下你应该在手持设备上创建通知并自动同步到穿戴式设备上.这可以使你仅构建一次通知而展现在多种不同设备上(不仅是穿戴设备上,还有电视),从而避免多次的设计.

如果标准的通知样式(比如:NotificationCompat.BigTextStyle或者NotificationCompat.InboxStyle)不能满足你的工作需求,你可以使用自定义布局去展示.你只能在穿戴式设备上创建和展示自定义通知,系统不会同步到手持设备上.

注意:当在穿戴式设备上创建自定义通知时,你可以用标准通知的API(API 20)代替支持库.

创建自定义通知

1.为Activity的内容创建一个自定义布局视图

public void onCreate(Bundle bundle){    ...    setContentView(R.layout.notification_activity);}

2.在清单文件中为Activity定义必要的属性,使其可以展示在穿戴式设备上下文中.你需要声明Activity的以下属性:exportable,allowEmbedded taskAFFinity,另外推荐使用Theme.DeviceDefault.Light主题.

<activity android:name="com.example.MyDisplayActivity"     android:exported="true"     android:allowEmbedded="true"     android:taskAffinity=""     android:theme="@android:style/Theme.DeviceDefault.Light" />

3.为希望展示的Activity创建隐式意图

Intent notificationIntent = new Intent(this, NotificationActivity.class);PendingIntent notificationPendingIntent = PendingIntent.getActivity(        this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

4.建立一个Notification并调用setDisplayIntent()方法.当用户触发点击这个通知后系统就会启动这个隐式意图.

5.用notify方法展示通知.

当通知在主屏幕上展示时,系统会用标准模版展示它.当用户向上滑动这个通知时,将会看到为通知设置的自定义视图.

使用Wearable UI Library创建布局


Wearable UI Library这个库在创建可穿戴式app的时候会被自动包括.也可以在在build.gradle中添加这个库.

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

这个库帮助你构建UI,更多信息可以参考Creating Custom UIs for Wear Devices.下面是这个类库中一些主要的类.

  • BoxInsetLayout
  • CardFragment
  • CircledImageView
  • ConfirmationActivity
  • CrossFadeDrawable
  • DelayedConfirmationView
  • DismissOverlayView
  • GridViewPager
  • GridPagerAdapter
  • FragmentGridPagerAdapter
  • DotsPageIndicator
  • WatchViewStub
  • WearableListView
0 0
原创粉丝点击