本页介绍了如何使用现有的 Glance 组件通过 Glance 处理大小以及提供灵活且响应迅速的布局。
使用 Box
、Column
和 Row
Glance 有三种主要的可组合项布局:
Box
:将元素叠放在另一个元素上。它会转换为RelativeLayout
。Column
:在纵轴上将元素一个接一个地放置。它会转换为垂直方向的LinearLayout
。Row
:在横轴上将元素一个接一个地放置。它会转换为水平方向的LinearLayout
。
Glance 支持 Scaffold
对象。将 Column
、Row
和 Box
可组合项放置在给定的 Scaffold
对象中。
每个可组合项都允许您使用修饰符定义其内容的垂直和水平对齐方式以及宽度、高度、粗细或内边距约束条件。此外,每个子级都可以定义其修饰符,以更改父级内的空间和位置。
以下示例展示了如何创建水平均匀分布其子项的 Row
,如图 1 所示:
Row(modifier = GlanceModifier.fillMaxWidth().padding(16.dp)) { val modifier = GlanceModifier.defaultWeight() Text("first", modifier) Text("second", modifier) Text("third", modifier) }
Row
会填充最大可用宽度,而且由于每个子项具有相同的权重,所以它们会均匀地共享可用空间。您可以定义不同的粗细、尺寸、内边距或对齐方式,以根据需要调整布局。
使用可滚动布局
提供自适应内容的另一种方法是使其可滚动。使用 LazyColumn
可组合项可以实现这一点。借助此可组合项,您可以定义要在应用 widget 中的可滚动容器内显示的一组项。
以下代码段展示了定义 LazyColumn
内项的不同方法。
您可以提供商品数量:
// Remember to import Glance Composables // import androidx.glance.appwidget.layout.LazyColumn LazyColumn { items(10) { index: Int -> Text( text = "Item $index", modifier = GlanceModifier.fillMaxWidth() ) } }
请提供各项内容:
LazyColumn { item { Text("First Item") } item { Text("Second Item") } }
提供项列表或数组:
LazyColumn { items(peopleNameList) { name -> Text(name) } }
您也可以组合使用上述示例:
LazyColumn { item { Text("Names:") } items(peopleNameList) { name -> Text(name) } // or in case you need the index: itemsIndexed(peopleNameList) { index, person -> Text("$person at index $index") } }
请注意,上一个代码段未指定 itemId
。从 Android 12 开始,指定 itemId
有助于通过列表和 appWidget
更新来提高性能并保持滚动位置(例如,在列表中添加或移除项时)。以下示例展示了如何指定 itemId
:
items(items = peopleList, key = { person -> person.id }) { person -> Text(person.name) }
定义 SizeMode
AppWidget
尺寸可能会因设备、用户选择或启动器而异,因此请务必提供灵活的布局,如提供灵活的 widget 布局页面中所述。Glance 使用 SizeMode
定义和 LocalSize
值来简化这一过程。以下部分介绍了三种模式。
SizeMode.Single
SizeMode.Single
是默认模式。它表示仅提供一种类型的内容;也就是说,即使 AppWidget
可用大小发生变化,内容大小也不会改变。
class MyAppWidget : GlanceAppWidget() { override val sizeMode = SizeMode.Single override suspend fun provideGlance(context: Context, id: GlanceId) { // ... provideContent { MyContent() } } @Composable private fun MyContent() { // Size will be the minimum size or resizable // size defined in the App Widget metadata val size = LocalSize.current // ... } }
使用此模式时,请确保:
- 根据内容大小正确定义最小和最大大小元数据值。
- 内容在预期尺寸范围内足够灵活。
通常,如果出现以下任一情况,您应使用此模式:
a) AppWidget
具有固定的尺寸,或者 b) 调整尺寸后不会更改其内容。
SizeMode.Responsive
此模式相当于提供自适应布局,可让 GlanceAppWidget
定义一组受特定尺寸限制的自适应布局。对于每个定义的尺寸,系统会在创建或更新 AppWidget
时创建内容并将其映射到特定尺寸。然后,系统会根据可用大小选择最适合的选项。
例如,在我们的目的地 AppWidget
中,您可以定义三种尺寸及其内容:
class MyAppWidget : GlanceAppWidget() { companion object { private val SMALL_SQUARE = DpSize(100.dp, 100.dp) private val HORIZONTAL_RECTANGLE = DpSize(250.dp, 100.dp) private val BIG_SQUARE = DpSize(250.dp, 250.dp) } override val sizeMode = SizeMode.Responsive( setOf( SMALL_SQUARE, HORIZONTAL_RECTANGLE, BIG_SQUARE ) ) override suspend fun provideGlance(context: Context, id: GlanceId) { // ... provideContent { MyContent() } } @Composable private fun MyContent() { // Size will be one of the sizes defined above. val size = LocalSize.current Column { if (size.height >= BIG_SQUARE.height) { Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp)) } Row(horizontalAlignment = Alignment.CenterHorizontally) { Button() Button() if (size.width >= HORIZONTAL_RECTANGLE.width) { Button("School") } } if (size.height >= BIG_SQUARE.height) { Text(text = "provided by X") } } } }
在前面的示例中,provideContent
方法被调用了三次,并映射到定义的大小。
- 在第一次调用中,大小的计算结果为
100x100
。内容不包含 extra 按钮,以及顶部和底部的文本。 - 在第二个调用中,大小的计算结果为
250x100
。内容包含额外按钮,但顶部和底部文本除外。 - 在第三个调用中,大小的计算结果为
250x250
。内容包括额外按钮和两种文字。
SizeMode.Responsive
是其他两种模式的组合,可让您在预定义边界内定义自适应内容。一般来说,此模式的性能更好,并且可在调整 AppWidget
大小时允许更顺畅的过渡。
下表根据 SizeMode
和 AppWidget
的可用大小显示了该大小的值:
可用尺寸 | 105 x 110 | 203 x 112 | 72 x 72 | 203 x 150 |
---|---|---|---|---|
SizeMode.Single |
110 x 110 | 110 x 110 | 110 x 110 | 110 x 110 |
SizeMode.Exact |
105 x 110 | 203 x 112 | 72 x 72 | 203 x 150 |
SizeMode.Responsive |
80 x 100 | 80 x 100 | 80 x 100 | 150 x 120 |
* 确切值仅用于演示目的。 |
SizeMode.Exact
SizeMode.Exact
相当于提供确切布局,每当可用的 AppWidget
大小发生变化时(例如,当用户在主屏幕上调整 AppWidget
的大小时)就会请求 GlanceAppWidget
内容。
例如,在目的地 widget 中,如果可用宽度大于特定值,则可以添加一个额外的按钮。
class MyAppWidget : GlanceAppWidget() { override val sizeMode = SizeMode.Exact override suspend fun provideGlance(context: Context, id: GlanceId) { // ... provideContent { MyContent() } } @Composable private fun MyContent() { // Size will be the size of the AppWidget val size = LocalSize.current Column { Text(text = "Where to?", modifier = GlanceModifier.padding(12.dp)) Row(horizontalAlignment = Alignment.CenterHorizontally) { Button() Button() if (size.width > 250.dp) { Button("School") } } } } }
此模式比其他模式提供更高的灵活性,但有一些注意事项:
- 每当大小发生变化时,都必须完全重新创建
AppWidget
。当内容复杂时,这可能会导致性能问题和界面跳转。 - 可用大小可能会因启动器的实现而异。例如,如果启动器未提供大小列表,系统会使用可能的最小大小。
- 在搭载 Android 12 之前版本的设备上,大小计算逻辑可能并非在所有情况下都适用。
通常,如果无法使用 SizeMode.Responsive
(即,一小部分自适应布局不可行),则应使用此模式。
访问资源
您可以使用 LocalContext.current
访问任何 Android 资源,如以下示例所示:
LocalContext.current.getString(R.string.glance_title)
我们建议您直接提供资源 ID,以缩减最终 RemoteViews
对象的大小并启用动态资源(例如动态配色)。
可组合项和方法接受使用“提供程序”(例如 ImageProvider
)或使用重载方法(例如 GlanceModifier.background(R.color.blue)
)的资源。例如:
Column( modifier = GlanceModifier.background(R.color.default_widget_background) ) { /**...*/ } Image( provider = ImageProvider(R.drawable.ic_logo), contentDescription = "My image", )
标识名文本
Glance 1.1.0 包含一个用于设置文本样式的 API。使用 TextStyle 类的 fontSize
、fontWeight
或 fontFamily
属性设置文本样式。
fontFamily
支持所有系统字体(如下例所示),但不支持应用中的自定义字体:
Text(
style = TextStyle(
fontWeight = FontWeight.Bold,
fontSize = 18.sp,
fontFamily = FontFamily.Monospace
),
text = "Example Text"
)
添加复合按钮
复合按钮是在 Android 12 中引入的。Glance 支持以下类型的复合按钮的向后兼容性:
每个复合按钮都会显示一个表示“选中”状态的可点击视图。
var isApplesChecked by remember { mutableStateOf(false) } var isEnabledSwitched by remember { mutableStateOf(false) } var isRadioChecked by remember { mutableStateOf(0) } CheckBox( checked = isApplesChecked, onCheckedChange = { isApplesChecked = !isApplesChecked }, text = "Apples" ) Switch( checked = isEnabledSwitched, onCheckedChange = { isEnabledSwitched = !isEnabledSwitched }, text = "Enabled" ) RadioButton( checked = isRadioChecked == 1, onClick = { isRadioChecked = 1 }, text = "Checked" )
当状态发生变化时,会触发所提供的 lambda。您可以存储检查状态,如以下示例所示:
class MyAppWidget : GlanceAppWidget() { override suspend fun provideGlance(context: Context, id: GlanceId) { val myRepository = MyRepository.getInstance() provideContent { val scope = rememberCoroutineScope() val saveApple: (Boolean) -> Unit = { scope.launch { myRepository.saveApple(it) } } MyContent(saveApple) } } @Composable private fun MyContent(saveApple: (Boolean) -> Unit) { var isAppleChecked by remember { mutableStateOf(false) } Button( text = "Save", onClick = { saveApple(isAppleChecked) } ) } }
您还可以为 CheckBox
、Switch
和 RadioButton
提供 colors
属性以自定义它们的颜色:
CheckBox( // ... colors = CheckboxDefaults.colors( checkedColor = ColorProvider(day = colorAccentDay, night = colorAccentNight), uncheckedColor = ColorProvider(day = Color.DarkGray, night = Color.LightGray) ), checked = isChecked, onCheckedChange = { isChecked = !isChecked } ) Switch( // ... colors = SwitchDefaults.colors( checkedThumbColor = ColorProvider(day = Color.Red, night = Color.Cyan), uncheckedThumbColor = ColorProvider(day = Color.Green, night = Color.Magenta), checkedTrackColor = ColorProvider(day = Color.Blue, night = Color.Yellow), uncheckedTrackColor = ColorProvider(day = Color.Magenta, night = Color.Green) ), checked = isChecked, onCheckedChange = { isChecked = !isChecked }, text = "Enabled" ) RadioButton( // ... colors = RadioButtonDefaults.colors( checkedColor = ColorProvider(day = Color.Cyan, night = Color.Yellow), uncheckedColor = ColorProvider(day = Color.Red, night = Color.Blue) ), )
其他组件
Glance 1.1.0 发布了其他组件,如下表所述:
名称 | 图片 | 参考链接 | 其他说明 |
---|---|---|---|
实心按钮 | 组件 | ||
轮廓按钮 | 组件 | ||
图标按钮 | 组件 | 主要 / 次要 / 仅图标 | |
标题栏 | 组件 | ||
Scaffold | Scaffold 和标题栏位于同一演示中。 |
如需详细了解设计细节,请参阅 Figma 上的此设计套件中的组件设计。