当用户更改手表设置中的字体大小时修改芯片大小。 Android Compose Wear OS Kotlin

问题描述 投票:0回答:1

当从手表设置更改字体大小时,我需要使芯片大小适应字体大小。我在 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() }
    }
}

这是将字体大小更改为最大设置时发生的情况:在此处输入图像描述

kotlin android-studio android-jetpack-compose wear-os
1个回答
0
投票

钟表师的芯片会为您处理这个问题,请参阅屏幕截图测试

添加依赖:

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),
)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.