我的螢屏目前分為 3 個主要部分(我想可能有更好的方法):


- 第一部分是展示資料。它分為9個幾乎相同的專案,以顯示不同的實時(定期更改資料)。它們現在是 3 行 3 列(我想可能有更好的方法)。


在它們下方,我有內部螢屏選項卡導航,它將控制哪個片段將顯示在螢屏的最后一部分。
在螢屏的底部,我想我需要 fragmentFrame(我將研究它在 Jetpack Compose 中的等效項),以及更頻繁更改的資料(資料顯示在圖表、地圖等中)。當在螢屏的第 2) 部分選擇特定選項卡時,將顯示每個片段。
這是我創建螢屏的代碼:
@ExperimentalFoundationApi
@Composable
fun ActivityCustomGridScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.Gray)
.fillMaxHeight(/*4f*/)
.fillMaxWidth(/*4f*/)
) {
CustomItem(1f, MaterialTheme.colors.secondaryVariant)
CustomItem(1f, MaterialTheme.colors.secondary)
CustomItem(1f, MaterialTheme.colors.onSurface)
CustomItem(0.7f, MaterialTheme.colors.onError, false)
CustomItem(2.5f, areItemsShown = false)
}
}
@Composable
fun ColumnScope.CustomItem(
weight: Float,
color: Color = MaterialTheme.colors.primary,
areItemsShown: Boolean = true
) {
Surface(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
// .width(200.dp)
.weight(weight)
.padding(3.dp),
color = color
) {
Row(
Modifier
.weight(3f)
) {
if (areItemsShown) {
ActivityCardComponent(
ParameterItemState(), modifier = Modifier
.padding(5.dp)
.weight(1f)
)
ActivityCardComponent(
ParameterItemState(), modifier = Modifier
.padding(5.dp)
.weight(1f),
Alignment.CenterHorizontally
)
ActivityCardComponent(
ParameterItemState(), modifier = Modifier
.padding(5.dp)
.weight(1f),
Alignment.End
)
}
}
}
}
- Also i have problems in creating singe Item from 1st part of the screen to be in desirable size (I want to have 3 Items per Column in this proportion 1:2:1)

@Composable
fun ActivityCardComponent(
itemData: ParameterItemState, modifier: Modifier,
alignment: Alignment.Horizontal = Start
) {
Card(
modifier = modifier
.wrapContentWidth(alignment)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.background(Color.Red)
) {
Image(
painter = painterResource(id = itemData.getIndicatorIcon()),
contentDescription = "Sport Parameter Icon"
)
Column(Modifier.padding(4.dp)) {
Text(
stringResource(itemData.getIndicatorTittle()),
modifier = Modifier
.align(Alignment.CenterHorizontally),
style = MaterialTheme.typography.overline,
)
Spacer(modifier = Modifier.defaultMinSize())
Text(
text = itemData.parameterValue,
modifier = Modifier,
style = MaterialTheme.typography.subtitle1
)
Text(
stringResource(itemData.getIndicatorDimensionName()),
modifier = Modifier.align(Alignment.End),
style = MaterialTheme.typography.overline
)
}
}
}
}
And my item state class:
data class ParameterItemState(
val parameterValue: String = "12 345",
val parameterDimension: IndicatorsEnum = IndicatorsEnum.DISTANCE_IN_METERS,
val itemVisibility: Int = View.VISIBLE,
val itemPosition: Int = 0,
) {
fun getIndicatorTittle() = parameterDimension.getIndicatorTittle() // Speed (Time per 500m) id
fun getIndicatorDimensionName() = parameterDimension.getIndicatorDimensionName() // ave per 500 m id
fun getIndicatorIcon() = parameterDimension.getIndicatorIcon() // icon id
}
I still didn't create viewmodel stuff, and i will accept answers including it too.
uj5u.com熱心網友回復:
看起來好簡單
@Composable
fun Test(){
Scaffold(Modifier.fillMaxSize()) {
Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)) {
BoxWithConstraints(Modifier.weight(1f)) {
Column(
Modifier.padding(8.dp).fillMaxSize(), verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val height = (this@BoxWithConstraints.maxHeight - 32.dp) / 3
Item(height)
Item(height)
Item(height)
}
}
LazyRow(contentPadding = PaddingValues(horizontal = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)){
items(5) { index ->
Box(
Modifier
.size(56.dp)
.clip(RoundedCornerShape(8.dp))
.background(Color.Yellow, RoundedCornerShape(8.dp)))
}
}
Box(
Modifier
.fillMaxSize()
.weight(1f)
.background(Color.Blue)) {
}
}
}
}
@Composable
fun Item(maxHeight: Dp){
Row(
Modifier
.fillMaxWidth()
.height(maxHeight),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Box(
Modifier
.fillMaxSize()
.weight(1f)
.background(Color.Red, RoundedCornerShape(8.dp))
.clip(RoundedCornerShape(8.dp))
)
Box(
Modifier
.fillMaxSize()
.weight(2f)
.background(Color.Red, RoundedCornerShape(8.dp))
.clip(RoundedCornerShape(8.dp))
)
Box(
Modifier
.fillMaxSize()
.weight(1f)
.background(Color.Red, RoundedCornerShape(8.dp))
.clip(RoundedCornerShape(8.dp))
)
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400090.html
標籤:android kotlin user-interface android-jetpack-compose android-components
