是否可以将Arrangement.spacedBy(16.dp)and组合Arrangement.Center在一个 中Row.horizontalArrangement?
我想做的是将内容水平居中并设置默认间距16.dp。
我知道我可以结合Row和 aBox来达到相同的结果,但我想知道是否可以仅使用 heRow的属性来完成。
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center, // I would like to add the .spacedBy(16.dp), keeping the Center arrangement
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = stringResource(R.string.generic_error_pagination),
style = MaterialTheme.typography.subtitle1,
color = MaterialTheme.colors.textSecondary,
)
OutlinedButton(onClick = onClick) {
Text(stringResource(id = R.string.retry_button))
}
}
Run Code Online (Sandbox Code Playgroud) android android-jetpack-compose android-jetpack-compose-layout android-compose-layout
如何使用可组合函数垂直对齐文本Text。有没有一种方法可以做到这一点,而不必添加另一个额外的视图来包含Text.
textAlign的参数只有Text以下选项:
TextAlign.
我尝试过使用textAlign = TextAlign.Center,但它只能水平居中。如何将其垂直居中而不将其包装在另一个视图中?
Text(
text = "Text",
modifier = Modifier.size(100.dp),
textAlign = TextAlign.Center
)
Run Code Online (Sandbox Code Playgroud)
结果:
我想要实现的目标:
android android-jetpack-compose android-jetpack-compose-layout android-compose-layout
我试图在单击文本时向下滚动到特定 UI 元素的位置。
我的文本的代码是:
Text(
"What is autosaving?",
color = colorResource(id = R.color.text_highlight),
fontSize = with(LocalDensity.current) {
dimensionResource(id = R.dimen._11ssp).toSp()
},
fontFamily = FontFamily(
Font(R.font.poppins_regular)
),
modifier = Modifier.constrainAs(whatIsAutosaving) {
top.linkTo(glWhatIsAutoSaving)
start.linkTo(parent.start)
end.linkTo(parent.end)
},
)
Run Code Online (Sandbox Code Playgroud)
单击此文本后,我的屏幕应滚动到另一个文本的开始位置。另一个文本的代码是:
Text(
stringResource(id = R.string.autosave_info),
color = colorResource(id = R.color.bright_green),
fontSize = with(LocalDensity.current) {
dimensionResource(id = R.dimen._11ssp).toSp()
}, fontFamily = FontFamily(
Font(R.font.poppins_regular)
),
modifier = Modifier.constrainAs(autoSaveInfo) {
top.linkTo(glAutoSaveInfo)
start.linkTo(glLeft)
end.linkTo(glRight)
width = Dimension.fillToConstraints
},
)
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?
编辑:
我的屏幕的完整代码是:
@Composable
fun Autosave(navController: NavController) {
val query …Run Code Online (Sandbox Code Playgroud) android scroll android-jetpack-compose android-jetpack-compose-layout android-compose-layout
我是jetpack compose的新手,我正在尝试做一件我无法实现的简单事情。
\n我想要做的是在同一行中对齐一个组件(在本例中是一个表面),位于行的开头,另一个组件(列)位于行的末尾。
\n怎么才能得到这个呢?
\n我正在尝试这个,但它不起作用:
\nRow(Modifier.padding(top = 24.dp)\n .fillMaxWidth()) {\n Surface(\n modifier = Modifier.size(70.dp),\n shape = RectangleShape,\n color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)\n ) {\n // Image goes here\n }\n\n Column(modifier = Modifier.size(70.dp)) {\n Text("Total",\n fontSize = 12.sp,\n color = AppGreyDark,\n modifier = Modifier.padding(end = 16.dp))\n\n Text("12,99 \xe2\x82\xac",\n fontSize = 18.sp,\n color = AppBlackDark,\n modifier = Modifier.padding(top = 4.dp))\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n android android-jetpack-compose composable android-compose-layout
我正在尝试创建一个具有嵌套滚动行为的撰写屏幕。为此,我使用lazyColumn作为父级,并且在其中我确实有多个带有layRow的行。
val scrollState = rememberLazyListState()
LazyColumn(
state = scrollState,
modifier = Modifier
.background(Color.White),
)
{
items(100, key = { it })
{
LazyRow(
contentPadding = PaddingValues(start = 16.dp, end = 16.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
items(10, key = { it }) {
Text(text = "Text $it")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我滚动列表时,我的行为出现滞后。我尝试创建发布版本并看到一些改进,但行为仍然显示出一些垃圾行为。不同类型的设备上的行为有所不同。
我尝试使用 Column 作为父视图,并看到了良好的改进,但它将列表中的所有视图加载在一起。这导致了一些其他延迟问题。
当我进行宏观基准测试时,发现了很多垃圾帧。其中大多数都请求渲染操作。
这里有什么优化建议吗?
android android-jetpack-compose android-jetpack-compose-material3 android-compose-layout
@Composable
fun TopAppBar(
name: String,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.fillMaxWidth()
.padding(20.dp, 0.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = "Back",
tint = Color.Black,
modifier = Modifier.size(24.dp)
)
Text(
text = name,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = 20.sp,
maxLines = 1
)
Row {
Icon(
painter = painterResource(id = R.drawable.ic_bell),
contentDescription = null,
tint = Color.Black,
modifier = Modifier
.padding(8.dp, 8.dp)
.size(24.dp)
)
Icon(
painter …Run Code Online (Sandbox Code Playgroud) android android-jetpack-compose android-jetpack-compose-row android-jetpack-compose-layout android-compose-layout
我想使用 chainStyle.Packed 将标题和描述文本链接到以图像为中心的位置,如何在 jetpack compose 中实现此目的。
当我使用createVerticalChain()其相对于父容器的创建链时,这不是我想要的,有没有办法实现这一点?
android android-constraintlayout android-jetpack-compose android-jetpack-compose-layout android-compose-layout
我一直试图将列中的按钮从底部到中心对齐一段时间,但我无法成功。
听到的是我的代码:
营养习惯筛查
@Composable
fun NutritionHabitsRoute(
navHostController: NavHostController,
sharedViewModel: SharedViewModel,
viewModel: NutritionHabitsViewModel = hiltViewModel()
) {
val state by viewModel.state.collectAsState()
NutritionHabit(
navHostController = navHostController,
sharedViewModel = sharedViewModel,
state = state,
buttonEnabled = viewModel::buttonEnabled
)
}
@Composable
fun NutritionHabit(
navHostController: NavHostController,
sharedViewModel: SharedViewModel,
state: NutritionHabitsScreenState,
buttonEnabled: (MutableList<Int>, MutableList<Int>) -> Unit
) {
val firstChoicesItems = sharedViewModel.registerItems?.choices?.get(0)?.items
val secondChoicesItems = sharedViewModel.registerItems?.choices?.get(1)?.items
var firstSelectedIndexes = remember { mutableListOf<Int>() }
var secondSelectedIndexes = remember { mutableListOf<Int>() }
Scaffold(
topBar = { BackPopUp(navController = navHostController, …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack-compose android-compose-layout
I've been trying to build a list with a Card in it formatted like this:
The difficulty here is that the title e.g. "Bread" and ingredient name e.g. "Flour" can be very long and thus I want to have an ellipsis to keep things manageable i.e. "My Long Flour name" will be displayed as "My Long Flou..." or as much space as is allowed. The picture size and the gram and percent widths are constant .dp values.
Ellipsis worked fine …
android android-jetpack-compose android-jetpack-compose-layout android-compose-layout