日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

JetPack?Compose底部導(dǎo)航欄的實(shí)現(xiàn)方法詳解_Android

作者:兒歌八萬首 ? 更新時(shí)間: 2022-11-01 編程語言

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è)置iconlabel,選中顏色和未選中顏色等等一些常用的屬性。可以給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

欄目分類
最近更新