網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
搭設(shè)基本Scaffold頁(yè)面
scaffold 組件遵循 Material Design,可以協(xié)助開(kāi)發(fā)者迅速構(gòu)建對(duì)應(yīng)框架頁(yè)面
準(zhǔn)備工作
首先在 drawable 文件夾內(nèi),添加幾張 vector images,用作我們的底部導(dǎo)航欄圖標(biāo)
在主頁(yè)面中聲明數(shù)據(jù)類,表示單個(gè)圖標(biāo)以及其解釋文本
data class Item( val name: String, val icon: Int )
新增組件 mainBody,逐一添加三個(gè)底部按鈕的圖標(biāo)
@Composable fun mainBody() { // 存儲(chǔ)當(dāng)前選中的底部按鈕的狀態(tài) var selectedItem by remember { mutableStateOf(0) } // 三個(gè)底部按鈕 val items = listOf( Item("主頁(yè)", R.drawable.home), Item("列表", R.drawable.list), Item("設(shè)置", R.drawable.setting) ) ... }
主體編寫
首先是設(shè)置 topBar,即頂部導(dǎo)航欄對(duì)應(yīng)按鈕
代碼很簡(jiǎn)單,但要注意使用的括號(hào)類型以及對(duì)應(yīng)嵌套關(guān)系!
Scaffold( topBar = { TopAppBar( title = { Text("主頁(yè)") }, navigationIcon = { IconButton(onClick = { /*TODO*/ }) { Icon(Icons.Filled.Menu, null) } } ) }, ... ){}
緊接著在 topBar
屬性后面寫底部導(dǎo)航欄屬性 bottomBar
items.forEachIndexed
按照索引渲染,vue 的 v-for
懂吧,就這個(gè)原理!
依次渲染 BottomNavigationItem
即可;
bottomBar = { BottomNavigation { items.forEachIndexed { index, item -> BottomNavigationItem( // selectedItem 是內(nèi)置屬性,表示當(dāng)前選中的Item // onClick即切換當(dāng)前激活的Item selected = selectedItem == index, onClick = { selectedItem = index }, // 這幾個(gè)屬性看看英文就懂了,不解釋 icon = { Icon(painterResource(item.icon), null) }, alwaysShowLabel = false, label = { Text(item.name) } ) } } }
這是總體的代碼:
@Composable fun mainBody() { var selectedItem by remember { mutableStateOf(0) } val items = listOf( Item("主頁(yè)", R.drawable.home), Item("列表", R.drawable.list), Item("設(shè)置", R.drawable.setting) ) Scaffold( topBar = { TopAppBar( title = { Text("主頁(yè)") }, navigationIcon = { IconButton(onClick = { /*TODO*/ }) { Icon(Icons.Filled.Menu, null) } } ) }, bottomBar = { BottomNavigation { items.forEachIndexed { index, item -> BottomNavigationItem( selected = selectedItem == index, onClick = { selectedItem = index }, icon = { Icon(painterResource(item.icon), null) }, alwaysShowLabel = false, label = { Text(item.name) } ) } } } ) { // 在scaffold里面塞一個(gè)box,糊弄一下 Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center ) { Text(text = "主頁(yè)界面") } } }
原文鏈接:https://blog.csdn.net/delete_you/article/details/128046107
相關(guān)推薦
- 2023-06-17 C#中Stopwatch的使用及說(shuō)明_C#教程
- 2022-09-09 C#流程控制詳解_C#教程
- 2022-02-02 ionic 禁止橫屏處理
- 2023-01-30 使用uniapp打包上架微信小程序完整教程_其它相關(guān)
- 2022-11-24 Linux學(xué)習(xí)之expect操作詳解_linux shell
- 2022-08-15 Spring框架常考知識(shí)點(diǎn)總結(jié)
- 2022-05-10 ioc基于注解方式
- 2022-02-20 C#?.NET實(shí)現(xiàn)掃描識(shí)別圖片中的文字_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支