Android Jetpack 可在各种设备和平台应用中实现经过优化的一致界面。Android Jetpack 包括 Wear OS 界面库,该界面库包含许多适用于 Wear OS 应用的首选界面组件。
Wear OS 界面库包括(但不限于)以下类:
-
BoxInsetLayout
。此布局可对圆形屏幕应用边衬区,这使开发者可以对不同的 Wear OS 屏幕形状使用相同的布局和数据。 -
SwipeDismissFrameLayout
。使用此布局,用户可通过在屏幕上从左向右滑动关闭任何视图。Wear OS 用户希望通过从左向右滑动屏幕来实现返回操作。 -
WearableRecyclerView
。此视图可提供曲线布局,如用于主 Wear OS 应用启动器的布局。 -
AmbientModeSupport
。此类与AmbientModeSupport.AmbientCallbackProvider
接口一起使用,共同为氛围模式提供支持。
如需查看完整列表,请参阅版本说明。
添加 Wear OS 界面库的依赖项
如需开始为应用创建叠加层,请创建特定于 Wear OS 的项目。然后将以下依赖项添加到应用的 build.gradle
文件中:
dependencies { ... // Standard Wear OS libraries implementation "androidx.wear:wear:1.1.0" // includes support for wearable specific inputs implementation "androidx.wear:wear-input:1.0.0" }
从 Wear OS 界面库软件包导入类
使用 Wear OS 界面库中的某个类时,可从 androidx.wear.widget
软件包导入该类。请参阅使用库类的示例。
在布局文件中使用正确的元素名称
在布局文件中,请使用与 Wear OS 界面库对应的完全限定名称。
例如,要使用 Wear OS 界面库中的 SwipeDismissFrameLayout
类,您可以在布局文件中指定以下内容:
<androidx.wear.widget.SwipeDismissFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/swipe_dismiss_root" > <TextView android:id="@+id/test_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="Swipe the screen to dismiss me." /> </androidx.wear.widget.SwipeDismissFrameLayout>