簡體   English   中英

沒有腳手架的jetpack compose中的TopAppBar給出錯誤

[英]TopAppBar in jetpack compose without scaffold gives error

TopAppBar(title = { Text(text = "Notes") },actions = {Icon(imageVector =Icons.Default.Notifications,contentDescription = "Notif",)},) {}

當我嘗試運行上述代碼時,出現以下錯誤“使用提供的 arguments 無法調用以下函數。” 當我查看 TopAppBar 的聲明時,有兩個同名的 Composable 但是只有我想要的 Composable 不能使用? 有小費嗎

發生這種情況是因為沒有與您提供的參數匹配的函數。 由於 function 過載,可能有幾個函數的參數略有不同。 避免歧義的最好方法是使用命名參數。

@Composable
fun TopAppBar(
    title: @Composable () -> Unit,
    modifier: Modifier = Modifier,
    navigationIcon: @Composable (() -> Unit)? = null,
    actions: @Composable RowScope.() -> Unit = {},
    backgroundColor: Color = MaterialTheme.colors.primarySurface,
    contentColor: Color = contentColorFor(backgroundColor),
    elevation: Dp = AppBarDefaults.TopAppBarElevation
) 

最接近你的在上面,可以設置為

TopAppBar(
    title = { Text(text = "Notes") },
    actions = {
        Icon(
            imageVector = Icons.Default.Notifications,
            contentDescription = "Notif",
        )
    }
)

或者你可以使用這個

@Composable
fun TopAppBar(
    modifier: Modifier = Modifier,
    backgroundColor: Color = MaterialTheme.colors.primarySurface,
    contentColor: Color = contentColorFor(backgroundColor),
    elevation: Dp = AppBarDefaults.TopAppBarElevation,
    contentPadding: PaddingValues = AppBarDefaults.ContentPadding,
    content: @Composable RowScope.() -> Unit
)

TopAppBar(
    // other params
) {
    Row(modifier = Modifier.fillMaxWidth()) {
        Text(text = "Notes")
        Spacer(modifier = Modifier.weight(1f))
        Icon(
            imageVector = Icons.Default.Notifications,
            contentDescription = "Notif"
        )
    }
}

只需刪除{}

TopAppBar(
    title = { Text(text = "Notes") },
    actions = {
        Icon(
            imageVector =Icons.Default.Notifications,
            contentDescription = "Notif",
        )},
) //{} remove this part

使用{}您嘗試使用具有屬性content: @Composable RowScope.() -> Unit沒有titleaction屬性的單元

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM