欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JetpackCompose Scaffold組件使用教程

 更新時(shí)間:2023年01月04日 15:34:12   作者:知奕奕  
在今年的Google/IO大會(huì)上,亮相了一個(gè)全新的 Android 原生 UI 開發(fā)框架-Jetpack Compose, 與蘋果的SwiftIUI一樣,Jetpack Compose是一個(gè)聲明式的UI框架

搭設(shè)基本Scaffold頁面

scaffold 組件遵循 Material Design,可以協(xié)助開發(fā)者迅速構(gòu)建對(duì)應(yīng)框架頁面

準(zhǔn)備工作

首先在 drawable 文件夾內(nèi),添加幾張 vector images,用作我們的底部導(dǎo)航欄圖標(biāo)

在主頁面中聲明數(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("主頁", R.drawable.home),
        Item("列表", R.drawable.list),
        Item("設(shè)置", R.drawable.setting)
    )
    ...
}

主體編寫

首先是設(shè)置 topBar,即頂部導(dǎo)航欄對(duì)應(yīng)按鈕

代碼很簡單,但要注意使用的括號(hào)類型以及對(duì)應(yīng)嵌套關(guān)系!

Scaffold(
    topBar = {
        TopAppBar(
            title = { Text("主頁") },
            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("主頁", R.drawable.home),
        Item("列表", R.drawable.list),
        Item("設(shè)置", R.drawable.setting)
    )
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("主頁") },
                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 = "主頁界面")
        }
    }
}

到此這篇關(guān)于JetpackCompose Scaffold組件使用教程的文章就介紹到這了,更多相關(guān)JetpackCompose Scaffold內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論