kotlin布局交互

将 wrapContentSize() 方法链接到 Modifier 对象,然后传递 Alignment.Center 作为实参以将组件居中。Alignment.Center 会指定组件同时在水平和垂直方向上居中。
kotlin 复制代码
DiceWithButtonAndImage(modifier = Modifier
    .fillMaxSize()
    .wrapContentSize(Alignment.Center)
)

创建垂直布局 在 Compose 中,垂直布局是使用 Column() 函数创建的

请在 Image 和 Button 可组合函数之间添加一个 Spacer 可组合函数。Spacer 接受 Modifier 作为形参。在本例中,Image 在 Button 上方,因此它们之间需要一定的垂直空间。因此,可以设置 Modifier 的高度以应用于 Spacer。尝试将高度设为 16.dp。通常,dp 尺寸以 4.dp 为增量进行更改。

kotlin 复制代码
Spacer(modifier = Modifier.height(16.dp))
###

构建掷骰子逻辑

默认情况下,可组合函数是无状态的,这意味着它们不存储值,并且可随时被系统重组,从而导致值被重置。不过,Compose 提供了一种避免这种情况的便捷方式。可组合函数可以使用 remember 可组合函数将对象存储在内存中。

在 remember 可组合函数正文中,传入 mutableStateOf() 函数,然后向该函数传递 1 实参。

mutableStateOf() 函数会返回一个可观察对象。稍后,您会详细了解可观察对象,但目前这基本上意味着,当 result 变量的值变化时,系统会触发重组、反映结果值并刷新界面。
var result by remember { mutableStateOf(1) }

kotlin 复制代码
注意:import androidx.compose.runtime.mutableStateOf 和 import androidx.compose.runtime.remember 语句会导入 mutableStateOf() 函数和 remember 可组合函数所需的软件包。

如需导入必要的 State 扩展函数,还需要以下 import 语句:

import androidx.compose.runtime.getValue

import androidx.compose.runtime.setValue
kotlin 复制代码
val imageResource = when (result) {
    1 -> R.drawable.dice_1
    2 -> R.drawable.dice_2
    3 -> R.drawable.dice_3
    4 -> R.drawable.dice_4
    5 -> R.drawable.dice_5
    else -> R.drawable.dice_6
}

完整代码

kotlin 复制代码
package com.example.diceroller

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.diceroller.ui.theme.DiceRollerTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
           DiceRollerTheme {
               DiceRollerApp()
           }
        }
    }
}



@Composable
fun DiceWithButtonAndImage(modifier: Modifier = Modifier){
    var result by remember { mutableStateOf(1) }
    val imageResource = when (result) {
        1 -> R.drawable.dice_1
        2 -> R.drawable.dice_2
        3 -> R.drawable.dice_3
        4 -> R.drawable.dice_4
        5 -> R.drawable.dice_5
        else -> R.drawable.dice_6
    }
    Column (
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(painterResource(imageResource),
            contentDescription = result.toString())
        Spacer(modifier = Modifier.height(16.dp))
        Button(onClick = { result = (1..6).random() }) {
            Text(stringResource(R.string.roll))
        }
    }
}

@Composable
@Preview
fun DiceRollerApp(){
    DiceWithButtonAndImage(modifier = Modifier
        .fillMaxSize()
        .wrapContentSize(Alignment.Center)
    )
}
相关推荐
top_designer1 小时前
Illustrato:钢笔工具“退休”了?Text to Vector 零基础矢量生成流
前端·ui·aigc·交互·ux·设计师·平面设计
樱桃园园长7 小时前
【Three.js 实战】手势控制 3D 奢华圣诞树 —— 从粒子系统到交互实现
javascript·3d·交互
Rysxt_7 小时前
Kotlin前景深度分析:市场占有、技术优势与未来展望
android·开发语言·kotlin
莫白媛7 小时前
Android开发之Kotlin 在 Android 开发中的全面指南
android·开发语言·kotlin
永亮同学1 天前
【探索实战】从零开始搭建Kurator分布式云原生平台:详细入门体验与功能实战分享!
分布式·云原生·交互
梓贤Vigo1 天前
【Axure原型分享】卡片排序
交互·产品经理·axure·原型·中继器
天勤量化大唯粉1 天前
基于距离的配对交易策略:捕捉价差异常偏离的均值回归机会(天勤量化代码实现)
android·开发语言·python·算法·kotlin·开源软件·策略模式
向宇it1 天前
【unity游戏开发——网络】unity对接steam,并上传发布游戏版本——Steamworks.NET
游戏·unity·游戏引擎·.net·交互
hudawei9961 天前
kotlin冷流热流的区别
android·开发语言·kotlin·flow··冷流·热流
hudawei9961 天前
对比kotlin和flutter中的异步编程
开发语言·flutter·kotlin·异步·