当从手表设置更改字体大小时,我需要使芯片大小适应字体大小。我在 Android Studio 中为 Wear OS 使用 compose。当手表设置中的字体大小设置为正常时,应用程序看起来不错,但是当手表设置中的字体大小更改为最大设置时,文本溢出,我最大的问题是芯片,就像它们一样不适应新的字体大小,文字溢出到芯片底部。所以,当这种情况发生时,我需要让芯片适应并调整大小。
这是我的代码:
fun MainScreen(
userPreferences: SharedPreferences,
oneTrainingsOnClick: () -> Unit,
freeTrainingOnClick: () -> Unit,
settingsOnClick: () -> Unit,
signInOnClick: () -> Unit
) {
val isUserLoggedIn = userPreferences.getBoolean(MainActivity.LOG_IN, false)
val chipColor = colorResource(id = R.color.gray)
val listState = rememberScalingLazyListState(initialCenterItemIndex = 0)
val coroutineScope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
Scaffold(
positionIndicator = { PositionIndicator(scalingLazyListState = listState) },
timeText = { TimeOnList(listState) },
vignette = { Vignette(vignettePosition = VignettePosition.TopAndBottom) }
) {
ScalingLazyColumn(
state = listState,
modifier = Modifier
.fillMaxSize()
.onRotaryScrollEvent {
coroutineScope.launch { listState.scrollBy(it.verticalScrollPixels) }
true
}
.focusRequester(focusRequester)
.focusable()
) {
item {
Chip(
icon = {
Image(
painter = painterResource(id = R.drawable.ic_one_icon),
contentDescription = null
)
},
onClick = {
if (isUserLoggedIn) oneTrainingsOnClick() else signInOnClick()
},
enabled = true,
colors = ChipDefaults.chipColors(chipColor),
modifier = Modifier
.wrapContentSize()
.fillMaxWidth()
.padding(top = 40.dp),
shape = RoundedCornerShape(30.dp),
label = {
Text(
text =
if (isUserLoggedIn) stringResource(id = R.string.one_trainings_button)
else stringResource(id = R.string.start),
style = TextStyle(
textAlign = TextAlign.Left,
fontFamily = Font(R.font.roboto_bold).toFontFamily(),
fontSize = dimensionResource(id = R.dimen.general_text).value.sp,
color = colorResource(id = R.color.white)
),
maxLines = 2,
modifier = Modifier.fillMaxWidth()
)
},
)
}
item {
Chip(
icon = {
Image(
painter = painterResource(id = R.drawable.ic_free_training),
contentDescription = null
)
},
onClick = { freeTrainingOnClick() },
colors = ChipDefaults.chipColors(chipColor),
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(30.dp),
label = {
Text(
text = stringResource(id = R.string.free_training_button),
style = TextStyle(
textAlign = TextAlign.Left,
fontFamily = Font(R.font.roboto_bold).toFontFamily(),
fontSize = dimensionResource(id = R.dimen.general_text).value.sp,
color = colorResource(id = R.color.white)
),
// overflow = TextOverflow.Ellipsis,
// maxLines = 1,
modifier = Modifier.fillMaxWidth()
)
}
)
}
item {
Chip(
icon = {
Image(
painter = painterResource(id = R.drawable.ic_settings),
contentDescription = null
)
},
onClick = { settingsOnClick() },
colors = ChipDefaults.chipColors(chipColor),
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(30.dp),
label = {
Text(
text = stringResource(id = R.string.configuration_text),
maxLines = 1,
style = TextStyle(
textAlign = TextAlign.Left,
fontFamily = Font(R.font.roboto_bold).toFontFamily(),
fontSize = dimensionResource(id = R.dimen.general_text).value.sp,
color = colorResource(id = R.color.white)
),
overflow = TextOverflow.Ellipsis,
modifier = Modifier.fillMaxWidth()
)
},
secondaryLabel = {
Text(text = stringResource(id = R.string.failed_authentication))
}
)
}
}
LaunchedEffect(Unit) { focusRequester.requestFocus() }
}
}
这是将字体大小更改为最大设置时发生的情况:在此处输入图像描述
添加依赖:
implementation "com.google.android.horologist:horologist-compose-material:<version>"
用途:
Chip(
label = stringResource(id = R.string.configuration_text),
onClick = { settingsOnClick() },
secondaryLabel = stringResource(id = R.string.failed_authentication)
icon = DrawableResPaintable(id = R.drawable.ic_settings),
)