Android Jetpack 包含 Wear OS UI 库。Wear OS UI 库包含以下类
-
CurvedTextView
:一个用于轻松编写文本的组件,该文本遵循可嵌入视图中的最大圆弧度。 -
DismissibleFrameLayout
:一种布局,允许用户通过按返回按钮或从左向右滑动屏幕来关闭任何视图。Wear OS 用户期望从左向右滑动执行返回操作。 -
WearableRecyclerView
:一个视图,它为使用WearableLinearLayoutManager
更新子布局提供基本的偏移逻辑。 -
AmbientModeSupport
:一个与AmbientModeSupport.AmbientCallbackProvider
接口一起使用的类,用于提供环境模式支持。
有关完整列表,请阅读发布说明。
添加对 Wear OS UI 库的依赖项
要开始创建应用,请创建一个 Wear-OS 特定的项目。然后将以下依赖项添加到您应用的 build.gradle
文件中
dependencies { ... // Standard Wear OS libraries implementation "androidx.wear:wear:1.2.0" // includes support for wearable specific inputs implementation "androidx.wear:wear-input:1.1.0" }
从 Wear OS UI 库包中导入类
要使用 Wear OS UI 库中的类,请从 androidx.wear.widget
包中导入它。
在布局文件中使用正确的元素名称
在布局文件中,使用与 Wear OS UI 库对应的完全限定名称。
例如,要使用 Wear OS UI 库中的 DismissibleFrameLayout
类,您可以在布局文件中指定以下内容
<androidx.wear.widget.DismissibleFrameLayout 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.DismissibleFrameLayout>