融会贯通Android Jetpack Compose中的Snackbar
正文
开始写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的资料请关注脚本之家其它相关文章!
相关文章
Android对话框AlertDialog.Builder使用方法详解
这篇文章主要介绍了Android对话框AlertDialog.Builder使用方法详解的相关资料,需要的朋友可以参考下2016-03-03Android RecyclerView自定义上拉和下拉刷新效果
这篇文章主要为大家详细介绍了Android RecyclerView自定义上拉和下拉刷新效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-02-02Android中ListView结合CheckBox实现数据批量选择(全选、反选、全不选)
这篇文章主要介绍了Android编程中ListView结合CheckBox实现批量选择,包括实现全选、反选、全不选等操作,感兴趣的小伙伴们可以参考一下2016-01-01自定义toast外形,多次点击不会总是弹出toast的实现方法
下面小编就为大家带来一篇自定义toast外形,多次点击不会总是弹出toast的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-04-04Android AsyncTask完全解析 带你从源码的角度彻底理解
这篇文章主要是针对Android AsyncTask进行完全解析,带你从源码的角度彻底理解,感兴趣的小伙伴们可以参考一下2016-04-04Android BroadcastReceiver接收收到短信的广播
这篇文章主要为大家详细介绍了Android BroadcastReceiver接收收到短信的广播,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-05-05
最新评论