網(wǎng)站首頁 編程語言 正文
1.聲明導(dǎo)航欄數(shù)據(jù)源
主要聲明導(dǎo)航欄label和圖標(biāo)數(shù)組,這里使用的是本地?cái)?shù)據(jù),也可以使用網(wǎng)絡(luò)數(shù)據(jù)。
//導(dǎo)航label數(shù)組 private val labels = arrayOf("首頁", "房訊", "消息", "我的") //導(dǎo)航默認(rèn)圖標(biāo)集合 private val defImages = arrayOf(R.mipmap.img_index, R.mipmap.img_info, R.mipmap.img_message, R.mipmap.img_preson) //導(dǎo)航選中圖標(biāo)集合 private var selectImages = arrayOf( R.mipmap.img_index_select, R.mipmap.img_info_select, R.mipmap.img_message_select, R.mipmap.img_preson_select ) //導(dǎo)航選中索引 private var selectIndex by mutableStateOf(0)
2.使用Scaffold搭建頁面結(jié)構(gòu)
Compose給我們提供了Scaffold
腳手架,實(shí)現(xiàn)了Material設(shè)計(jì)的頁面基本結(jié)構(gòu)。包括標(biāo)題欄、底部欄、SnackBar(類似吐司功能)、浮動(dòng)按鈕、抽屜組件、剩余內(nèi)容布局等,讓我們可以快速定義一個(gè)基本的頁面結(jié)構(gòu)。
setContent { val navController = rememberNavController() Scaffold( //設(shè)置底部導(dǎo)航欄 bottomBar = { BuildBottomBar(labels = labels, navController) }, ) { //設(shè)置頁面主內(nèi)容區(qū)域,這里通過NavHost,根據(jù)選中不同的導(dǎo)航欄Tab導(dǎo)航到不同的頁面。 NavHost(navController = navController, startDestination = labels[selectIndex]) { composable(labels[0]) { //首頁Compose IndexPage() } composable(labels[1]) { //咨詢Compose InfoPage() } composable(labels[2]) { //消息Compose MessagePage() } composable(labels[3]) { //個(gè)人中心Compose PersonPage() } } } }
3.BottomNavigation的用法
BottomNavigation
中的content可以添加多個(gè)BottomNavigationItem
,用來構(gòu)建導(dǎo)航欄的樣式,BottomNavigationItem
中可以設(shè)置icon
和label
,選中顏色和未選中顏色等等一些常用的屬性。可以給item設(shè)置點(diǎn)擊事件onClick
等。
BottomNavigation(backgroundColor = Color.White, elevation = 6.dp) { for (i in labels.indices) { BottomNavigationItem(selected = selectIndex == i, onClick = { selectIndex = i navController.navigate(labels[i]) }, icon = { Image( painter = painterResource(id = if (selectIndex == i) selectImages[i] else defImages[i]), contentDescription = labels[i], modifier = Modifier.size(25.dp) ) }, label = { Text(text = labels[i], color = if (selectIndex == i) Color.Red else Color.Gray) }) } }
這里用了一個(gè)循環(huán)來添加BottomNavigationItem
,通過selectIndex
來判斷tab是否選中。在點(diǎn)擊事件中設(shè)置選中的索引。 這里要注意的是在Compose中導(dǎo)航主要是用NavHostController
來進(jìn)行控制。 需要引入單獨(dú)的依賴庫
通常這個(gè)實(shí)例是用rememberNavController()
來獲取。
val navController = rememberNavController()
可以通過navigate
方法來進(jìn)行導(dǎo)航到具體也面,naviget
傳人的參數(shù)和NavHost
中的startDestination
參數(shù)一致,這樣就可以跳轉(zhuǎn)到指定頁面中
navController.navigate(labels[i])
原文鏈接:https://blog.csdn.net/cj641809386/article/details/120704746
相關(guān)推薦
- 2022-12-05 服務(wù)啟動(dòng)項(xiàng)Start類型詳解_其它
- 2022-09-06 C語言常見排序算法之交換排序(冒泡排序,快速排序)_C 語言
- 2023-04-07 Android開發(fā)基礎(chǔ)簡化Toast調(diào)用方法詳解_Android
- 2022-02-13 使用filter過濾器計(jì)算數(shù)組中符合條件的長度
- 2023-02-06 Python實(shí)現(xiàn)號(hào)碼歸屬地查詢功能_python
- 2022-07-22 Mybatis為實(shí)體類自定義別名的兩種方式
- 2024-03-04 JQ實(shí)現(xiàn)將div的滾動(dòng)條滾動(dòng)到指定子元素所在的位置
- 2022-11-05 關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton_python
- 最近更新
-
- 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)證過濾器
- 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)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支