iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >融会贯通Android Jetpack Compose中的Snackbar
  • 152
分享到

融会贯通Android Jetpack Compose中的Snackbar

Android Jetpack Compose SnackbarJetpack Compose 2023-01-11 12:01:08 152人浏览 泡泡鱼
摘要

目录正文主要的实现思路Snackbar UI部分正文 开始写Compose的时候,真的有点不习惯。思考方式和以前完全不同,有点类似ReactNative。 写习惯了之后,还真有点

正文

开始写Compose的时候,真的有点不习惯。思考方式和以前完全不同,有点类似ReactNative。 写习惯了之后,还真有点欲罢不能,行云流水~

Snackbar感觉就是Toast Plus版,可以自定义视图,还可以进行交互,可以用在很多地方实现意想不到的效果。

主要的实现思路

主要的实现思路有两部步:

  • 1.Snackbar的控制逻辑
  • 2.Snackbar的UI部分

Snackbar UI部分

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        actionBar?.hide()
        setContent {
            ContentView(Modifier.fillMaxSize())
        }
    }
}
@Composable
fun ContentView(modifier: Modifier) {
    val context = LocalContext.current
    val snackBarState = remember {
        SnackbarHostState()
    }
    val coroutineScope = rememberCoroutineScope()
    Surface(
        color = Color.DarkGray,
        modifier = modifier.fillMaxSize()
    ) {
        Box(modifier = modifier.fillMaxSize()) {
            Button(
                modifier = modifier
                    .align(Alignment.Center)
                    .wrapContentSize()
                ,
                onClick = {
                    coroutineScope.launch {//showSnackbar为suspend函数,要在协成调用
                        snackBarState.showSnackbar("")
                    }
                },
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = Color.White,
                    contentColor = Color.Black
                )
            ) {
                Text(text = "显示SnackBar", fontSize = 16.sp)
            }
            SnackbarHost(
                modifier = modifier.align(Alignment.BottomCenter),
                hostState = snackBarState
            ) {
                // custom snackBar
                CustomSnackBar(
                    title = "我是绿色大米呀",
                    content = "关注点一波~下次不迷路哦!",
                    profileImageResource = R.drawable.head,
                    actionImageResource = R.drawable.back_black_bg,
                    onAction = {}
                )
            }
        }
    }
}
@Composable
fun CustomSnackBar(
    modifier: Modifier = Modifier,
    title: String,
    content: String,
    profileImageResource: Int,
    actionImageResource: Int,
    onAction: () -> Unit
) {
    Snackbar(
        elevation = 0.dp,//去掉阴影
        backgroundColor = Color.Transparent
    ) {
        Box(
            modifier = modifier
                .fillMaxWidth()
                .wrapContentHeight()
        ) {
            Column(
                modifier = modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth()
                    .clip(RoundedCornerShape(10.dp))
                    .background(
                        Brush.verticalGradient(
                            colors = listOf(
                                Color(Android.graphics.Color.parseColor("#0ac1ff")),
                                Color(android.graphics.Color.parseColor("#fb2c38"))
                            )
                        )
                    )
                    .padding(start = 78.dp, top = 8.dp, bottom = 12.dp, end = 8.dp),
                horizontalAlignment = Alignment.Start
            ) {
                Text(
                    modifier = modifier.
                        padding(top = 5.dp),
                    text = title,
                    color = Color.White,
                    fontWeight = FontWeight.Bold,
                    fontSize = 22.sp
                )
                Spacer(modifier = modifier.padding(vertical = 2.dp))
                Text(
                    text = content,
                    color = Color.White,
                    fontStyle = FontStyle.Italic,
                    fontSize = 15.sp
                )
            }
            Column(
                modifier = modifier
                    .padding(start = 16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Card(
                    elevation = 6.dp,
                    shape = RoundedCornerShape(8.dp)
                ) {
                    Image(
                        painter = painterResource(profileImageResource),
                        contentScale = ContentScale.Crop,
                        contentDescription = null,
                        modifier = modifier.size(50.dp)
                    )
                }
            }
            Image(
                painter = painterResource(actionImageResource),
                contentDescription = null,
                contentScale = ContentScale.Fit,
                modifier = modifier
                    .align(Alignment.BottomEnd)
                    .padding(bottom = 10.dp, end = 10.dp)
                    .size(23.dp)
                    .rotate(180f)
                    .clickable(
                        interactionSource = MutableInteractionSource(),
                        indication = null,
                        onClick = onAction
                    )
            )
        }
    }
}

不知道为什么,我的这个Snackbar一直出现在屏幕顶部,如果想让它出现在底部应该如何实现?更多关于Android Jetpack Compose Snackbar的资料请关注编程网其它相关文章!

--结束END--

本文标题: 融会贯通Android Jetpack Compose中的Snackbar

本文链接: https://www.lsjlt.com/news/177406.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作