当前位置: 首页 > news >正文

kotlin布局交互

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

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

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

Spacer(modifier = Modifier.height(16.dp))
###

构建掷骰子逻辑

默认情况下,可组合函数是无状态的,这意味着它们不存储值,并且可随时被系统重组,从而导致值被重置。不过,Compose 提供了一种避免这种情况的便捷方式。可组合函数可以使用 remember 可组合函数将对象存储在内存中。
在 remember 可组合函数正文中,传入 mutableStateOf() 函数,然后向该函数传递 1 实参。
mutableStateOf() 函数会返回一个可观察对象。稍后,您会详细了解可观察对象,但目前这基本上意味着,当 result 变量的值变化时,系统会触发重组、反映结果值并刷新界面。
var result by remember { mutableStateOf(1) }

注意:import androidx.compose.runtime.mutableStateOf 和 import androidx.compose.runtime.remember 语句会导入 mutableStateOf() 函数和 remember 可组合函数所需的软件包。如需导入必要的 State 扩展函数,还需要以下 import 语句:import androidx.compose.runtime.getValueimport androidx.compose.runtime.setValue
val imageResource = when (result) {1 -> R.drawable.dice_12 -> R.drawable.dice_23 -> R.drawable.dice_34 -> R.drawable.dice_45 -> R.drawable.dice_5else -> R.drawable.dice_6
}

完整代码

package com.example.dicerollerimport 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.DiceRollerThemeclass 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_12 -> R.drawable.dice_23 -> R.drawable.dice_34 -> R.drawable.dice_45 -> R.drawable.dice_5else -> 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))
}

文章转载自:
http://arride.lbooon.cn
http://calamint.lbooon.cn
http://asthmatic.lbooon.cn
http://arsenical.lbooon.cn
http://cainogenesis.lbooon.cn
http://cardioverter.lbooon.cn
http://capework.lbooon.cn
http://cabotin.lbooon.cn
http://chihuahua.lbooon.cn
http://beating.lbooon.cn
http://canadien.lbooon.cn
http://blackhearted.lbooon.cn
http://bastardly.lbooon.cn
http://cervicothoracic.lbooon.cn
http://auriform.lbooon.cn
http://accounting.lbooon.cn
http://breakage.lbooon.cn
http://aerophile.lbooon.cn
http://aerodynamically.lbooon.cn
http://cementation.lbooon.cn
http://bidding.lbooon.cn
http://becrawl.lbooon.cn
http://camembert.lbooon.cn
http://affiant.lbooon.cn
http://anklet.lbooon.cn
http://armalcolite.lbooon.cn
http://asparagine.lbooon.cn
http://amplitude.lbooon.cn
http://breadbasket.lbooon.cn
http://amazing.lbooon.cn
http://www.dtcms.com/a/280320.html

相关文章:

  • Kotlin聚合方法
  • Python 操作Excel工作表:添加、删除、移动、隐藏
  • 前端安全指南:防御XSS与CSRF攻击
  • 给 Excel 整列空格文字内容加上前缀:像给文字穿衣服一样简单!
  • Excel制作玫瑰图
  • PostgreSQL FATAL: sorry, too many clients already 连接数爆满的处理办法
  • excel 通过openpyxl表格下载和插入图片
  • 京东平台商品评论接口接入指南与代码实现
  • 国内大模型技术与应用综述
  • 区块链:以太坊侧链Polygon
  • 日常运维问题汇总-59
  • STL的一些知识点
  • C/C++宏定义中do{}while(0)的妙用
  • CAS单点登录架构详解
  • 弗兰肯斯坦式的人工智能与GTM策略的崩溃
  • (LeetCode 每日一题) 3136. 有效单词 (字符串)
  • 【牛客LeetCode数据结构】单链表的应用——移除链表元素问题、链表分割问题详解
  • 从零构建鸿蒙应用:深度解析应用架构与项目结构
  • MIPI DSI(五) DBI 和 DPI 格式
  • 3.2数据库-关系代数-函数依赖-范式
  • Pitaya 是一个简单、快速、轻量级的游戏服务器框架,它为分布式多人游戏和服务器端应用程序提供了一个基本的开发框架
  • java的BO VO PO DO等对象的统称
  • 【Numba】正确使用numba,让你的python代码原地起飞!
  • 【转】Rust: PhantomData,#may_dangle和Drop Check 真真假假
  • 022_提示缓存与性能优化
  • 程序“夯住“的常见原因
  • 在物联网系统中时序数据库和关系型数据库如何使用?
  • 深入掌握Python正则表达式:re库全面指南与实战应用
  • .NET 10 Preview 1发布
  • OpenCV多尺度图像增强算法函数BIMEF()