融會貫通Android?Jetpack?Compose中的Snackbar
正文
開始寫Compose的時候,真的有點不習慣。思考方式和以前完全不同,有點類似ReactNative。 寫習慣了之后,還真有點欲罷不能,行云流水~
Snackbar感覺就是Toast Plus版,可以自定義視圖,還可以進行交互,可以用在很多地方實現(xiàn)意想不到的效果。
主要的實現(xiàn)思路
主要的實現(xiàn)思路有兩部步:
- 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函數(shù),要在協(xié)成調(diào)用 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 = "關(guān)注點一波~下次不迷路哦!", 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一直出現(xiàn)在屏幕頂部,如果想讓它出現(xiàn)在底部應(yīng)該如何實現(xiàn)?更多關(guān)于Android Jetpack Compose Snackbar的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Android中visibility屬性VISIBLE、INVISIBLE、GONE的區(qū)別
在Android開發(fā)中,大部分控件都有visibility這個屬性,其屬性有3個分別為“visible ”、“invisible”、“gone”。主要用來設(shè)置控制控件的顯示和隱藏。本文就詳細的講解一下。2016-12-12Android工具欄頂出轉(zhuǎn)場動畫的實現(xiàn)方法實例
這篇文章主要給大家介紹了關(guān)于Android工具欄頂出轉(zhuǎn)場動畫的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09Android Glide圖片加載(加載監(jiān)聽、加載動畫)
這篇文章主要為大家詳細介紹了Android Glide圖片加載的具體實現(xiàn)方法,包括加載監(jiān)聽、加載動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析
這篇文章主要為大家介紹了Flutter應(yīng)用程序?qū)崿F(xiàn)隱私屏幕示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Android使用CountDownTimer實現(xiàn)倒計時效果
這篇文章主要為大家詳細介紹了Android使用CountDownTimer實現(xiàn)倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換
這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換效果的方法,PagerSlidingTabStrip是GitHub上的一個開源項目,調(diào)用這個庫可以少寫不少代碼XD 需要的朋友可以參考下2016-04-04android編程開發(fā)之全屏和退出全屏的實現(xiàn)方法
這篇文章主要介紹了android編程開發(fā)之全屏和退出全屏的實現(xiàn)方法,以實例形式較為詳細的分析了Android全屏及退出全屏的頁面布局與功能實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11