compose——侧边栏

效果图:

依赖引入:

Kotlin 复制代码
implementation 'com.google.android.material:material:1.5.0'

代码如下:

Kotlin 复制代码
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposedrawerTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    MainPage()
                }
            }
        }
    }
}

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainPage() {
    val scaffoldState = rememberScaffoldState()
    val coroutineScope = rememberCoroutineScope()// 定义协程作用域

    Scaffold(
        topBar = {
            TopAppBar(
                navigationIcon = {
                    IconButton(
                        onClick = {
                            coroutineScope.launch {
                                scaffoldState.drawerState.open()
                            }
                        }
                    ) {
                        Icon(Icons.Filled.Menu, null)
                    }
                },
                title = {
                    Text("首页")
                },
                actions = {
                    IconButton(
                        onClick = {}
                    ) {
                        Icon(Icons.Filled.Share, null)
                    }
                    IconButton(
                        onClick = {}
                    ) {
                        Icon(Icons.Filled.Settings, null)
                    }
                },
            )
        },
        drawerContent = { DrawerContent() },
        drawerGesturesEnabled = true,
        scaffoldState = scaffoldState,
    ) {

    }
}

@Composable
fun DrawerContent() {
    Text(text = "DrawerContent")
}
相关推荐
董三毛2 小时前
Kotlin Coroutine 底层实现原理
android
L108702 小时前
AutoJsPro GoogleMaterial3 M3组件使用示例
android
枣把儿3 小时前
「zotepad」用Gemini3pro写出一个高效写作和发文的记事本应用
android·前端·nuxt.js
明川3 小时前
Android Gradle 学习 - 生命周期和Task
android·前端·gradle
技术摆渡人4 小时前
Android 系统技术探索(5)指尖的舞蹈(Input 系统与 ANR)
android
来碗疙瘩汤4 小时前
uniapp动态读取版本号
android
用户41659673693554 小时前
存量项目如何拥抱 KMP?从环境搭建到组件化集成的保姆级指南
android
技术摆渡人5 小时前
Android 系统技术探索(3)光影魔术(SurfaceFlinger & 图形栈)。
android
某空m6 小时前
【Android】浅析DataBinding
android·开发语言
sky北城7 小时前
You are not able to choose some of the languages, because locales for them a
android