Android NDK开发详解Wear之切换条状标签和时间文本

Android NDK开发详解Wear之切换条状标签和时间文本

切换条状标签

ToggleChip
Added in 1.0.0


fun ToggleChip(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    label:  RowScope.() -> Unit,
    toggleControl:  () -> Unit,
    modifier: Modifier = Modifier,
    appIcon: ( BoxScope.() -> Unit)? = null,
    secondaryLabel: ( RowScope.() -> Unit)? = null,
    colors: ToggleChipColors = ToggleChipDefaults.toggleChipColors(),
    enabled: Boolean = true,
    interactionSource: MutableInteractionSource = remember {
    
     MutableInteractionSource() },
    contentPadding: PaddingValues = ToggleChipDefaults.ContentPadding,
    shape: Shape = MaterialTheme.shapes.small
): Unit

A ToggleChip is a specialized type of Chip that includes a slot for a bi-state toggle control such as a radio button, toggle or checkbox.

The Wear Material ToggleChip offers four slots and a specific layout for an application icon, a label, a secondaryLabel and toggle control. The application icon and secondaryLabel are optional. The items are laid out in a row with the optional icon at the start, a column containing the two label slots in the middle and a slot for the toggle control at the end.

The ToggleChip is Stadium shaped and has a max height designed to take no more than two lines of text of Typography.button style. With localisation and/or large font sizes, the ToggleChip height adjusts to accommodate the contents. The label and secondary label should be consistently aligned.

The recommended set of ToggleChipColors can be obtained from ToggleChipDefaults, e.g. ToggleChipDefaults.toggleChipColors.

Chips can be enabled or disabled. A disabled chip will not respond to click events.

Example of a ToggleChip with an icon, label and secondary label (defaults to switch toggle):

import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.semantics
import androidx.wear.compose.material.Icon
import androidx.wear.compose.material.Switch
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.ToggleChip

var checked by remember {
    
     mutableStateOf(true) }
// When we have both label and secondary label present limit both to 1 line of text
ToggleChip(
    label = {
    
    
        Text("SwitchIcon", maxLines = 1, overflow = TextOverflow.Ellipsis)
    },
    secondaryLabel = {
    
    
        Text("With secondary label", maxLines = 1, overflow = TextOverflow.Ellipsis)
    },
    checked = checked,
    // For Switch  toggle controls the Wear Material UX guidance is to set the
    // unselected toggle control color to ToggleChipDefaults.switchUncheckedIconColor()
    // rather than the default.
    colors = ToggleChipDefaults.toggleChipColors(
        uncheckedToggleControlColor = ToggleChipDefaults.SwitchUncheckedIconColor
    ),
    toggleControl = {
    
    
        Switch(
            checked = checked,
            enabled = true,
            modifier = Modifier.semantics {
    
    
                this.contentDescription =
                    if (checked) "On" else "Off"
            }
        )
    },
    onCheckedChange = {
    
     checked = it },
    appIcon = {
    
    
        Icon(
            painter = painterResource(id = R.drawable.ic_airplanemode_active_24px),
            contentDescription = "airplane",
            modifier = Modifier.size(24.dp).wrapContentSize(align = Alignment.Center),
        )
    },
    enabled = true,
)

ToggleChip 是一种可以让用户选择不同选项的专用条状标签。
在这里插入图片描述

切换条状标签
切换条状标签包含一个用于在两种状态之间进行切换的控件,例如开关、单选按钮或复选框。在某些情况下(例如在“设置”中),您可能需要快速方便地设置许多选项,此时就应使用切换条状标签。
在这里插入图片描述

剖析

在这里插入图片描述

切换条状标签有四个槽位,可容纳两个文本标签、一个选择控件和一个应用图标。图标和次级标签可选。
A. 标签

B. 次级标签

C. 图标

D. 选择控件

E. 容器

选择控件
在这里插入图片描述

开关
开关用于开启或关闭选择。

在这里插入图片描述

单选按钮
单选按钮适用于只能让用户选择一个选项的列表。

在这里插入图片描述

复选框
复选框适用于用户可以选择多个选项的列表。

相关组件

拆分式切换条状标签
SplitToggleChip 与 ToggleChip 的不同之处在于,它有两个可点按区域:一个可点击的文本区域和一个带切换开关的区域。
在这里插入图片描述

在拆分式切换条状标签上,通过对每个部分采用不同的颜色来区分可点按背景区域和切换控件。

用法

切换条状标签的用法如以下示例所示。

在这里插入图片描述

时间文本

TimeText 是一种在屏幕顶部显示当前时间的布局。

在这里插入图片描述

时间文本
使用时间文本可在屏幕顶部显示时间以及可选标签。当设备配备圆形屏幕时,时间文本为曲线样式。 当设备配备矩形屏幕时,时间文本为直线样式。
在这里插入图片描述

您可以为时间文本添加额外的前导内容标签。添加前导内容时,弧线的完整长度不应超过表盘的四分之一。

剖析

在这里插入图片描述

使用列表等可滚动元素创建 TimeText 时,请将 TimeText 设计为在元素滚动时淡出。使用 Modifier.scrollAway,根据滚动状态垂直滚动 TimeText,使其显示/不显示在视图中。
A. 前导内容
B. 分隔符
C. 时间

用法

如需在地图应用中显示预计到达时间,请使用包含前导内容的时间文本,如以下示例所示。
在这里插入图片描述

本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。

最后更新时间 (UTC):2022-12-20。

猜你喜欢

转载自blog.csdn.net/hnjzfwy/article/details/134867388