如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

huangapple go评论72阅读模式
英文:

How to Align the Icon to the End in TopAppBar jetpack compose

问题

现在图标始终在开头,我该如何将图标对齐到末尾?我尝试使用修饰符但不起作用。提前谢谢。

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

英文:

Now the icon always in the start, what should I do to align the icon to the end? I tried to use modifier but doesn't work. Thank you in advance.

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

答案1

得分: 1

如果您想在TopAppBar中使Icon对齐到末尾,请使用actions参数,而不是navigationIcon

示例代码如下:

TopAppBar(
    title = { Text("Simple TopAppBar") },
    backgroundColor = Red,
    actions = {
        // 在这里使用RowScope,以便这些图标水平放置
        IconButton(onClick = { /* doSomething() */ }) {
            Icon(Icons.Filled.Close, contentDescription = null)
        }
    }
)

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

英文:

If you want an Icon aligned at the end in the TopAppBar use the actions parameter instead of the navigationIcon.

Something like:

TopAppBar(
    title = { Text("Simple TopAppBar") },
    backgroundColor = Red,
    actions = {
        // RowScope here, so these icons will be placed horizontally
        IconButton(onClick = { /* doSomething() */ }) {
            Icon(Icons.Filled.Close, contentDescription = null)
        }
    }
)

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

答案2

得分: 1

App顶部栏示例

确保已添加M3依赖项:

implementation 'androidx.compose.material3:material3:1.1.0-rc01'

为顶部栏创建独立函数

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBarApp(context: Context, title: String, navigationIconClick: () -> Unit) {
    TopAppBar(
        title = {
            Text(text = title)
        },
        navigationIcon = {
            IconButton(onClick = navigationIconClick) {
                Icon(
                    imageVector = Icons.Filled.ArrowBack,
                    contentDescription = "导航图标"
                )
            }
        },
        actions = {
            // 在这里使用RowScope,以便这些图标水平放置
            IconButton(onClick = { context.showToast("您点击了搜索图标") }) {
                Icon(Icons.Filled.Search, contentDescription = null)
            }
            IconButton(onClick = { context.showToast("您点击了分享图标") }) {
                Icon(Icons.Filled.Share, contentDescription = null)
            }
            IconButton(onClick = { context.showToast("您点击了设置图标") }) {
                Icon(Icons.Filled.Settings, contentDescription = null)
            }
        },
        colors = topAppBarColors(
            containerColor = Purple80,
            scrolledContainerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp)
        )
    )
}

从视图中调用:

// 添加顶部栏
TopBarApp(context = baseContext, "用户列表"){
    baseContext.showToast("您点击了返回箭头")
}

输出:

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

希望这对您有帮助,祝您一天愉快。

英文:

App top bar example

make sure added M3 dependencies :

implementation 'androidx.compose.material3:material3:1.1.0-rc01'

Created seprate function for topbar

 @OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBarApp(context: Context, title: String, navigationIconClick: () -> Unit) {
    TopAppBar(
        title = {
            Text(text = title)
        },
        navigationIcon = {
            IconButton(onClick = navigationIconClick) {
                Icon(
                    imageVector = Icons.Filled.ArrowBack,
                    contentDescription = "Navigation icon"

                )
            }

        },
        actions = {
            // RowScope here, so these icons will be placed horizontally
            IconButton(onClick = { context.showToast("you clicked search icon") }) {
                Icon(Icons.Filled.Search, contentDescription = null)
            }
            IconButton(onClick = { context.showToast("you clicked share icon") }) {
                Icon(Icons.Filled.Share, contentDescription = null)
            }
            IconButton(onClick = { context.showToast("you clicked setting icon") }) {
                Icon(Icons.Filled.Settings, contentDescription = null)
            }
        },

        colors = topAppBarColors(
            containerColor = Purple80,
            scrolledContainerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp)
        )
    )

}

Call it from View :

// add top bar
                        TopBarApp(context = baseContext, "User List"){
                            baseContext.showToast("u clicked arrow back")
                        }

Output :

如何在Jetpack Compose的TopAppBar中将图标对齐到末尾?

hope it will work for you, have a good day.

huangapple
  • 本文由 发表于 2023年2月7日 04:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定