使用 Wear OS 界面库

Android Jetpack 可在各种设备和平台应用中实现经过优化的一致界面。Android Jetpack 包括 Wear OS 界面库,该界面库包含许多适用于 Wear OS 应用的首选界面组件。

Wear OS 界面库包括(但不限于)以下类:

如需查看完整列表,请参阅版本说明

添加 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>