How to show Snackbar at top of the screen

后端 未结 9 1658
梦谈多话
梦谈多话 2020-12-02 07:33

As the Android documentation says

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom

9条回答
  •  醉梦人生
    2020-12-02 08:12

    to show Snackbar at top of the screen (kotlin)

    // Custom snackbar : banner model
                val customSnackBar   = Snackbar.make(snackbar_id, "", Snackbar.LENGTH_INDEFINITE)
    
                val view = customSnackBar.view
                val params = view.layoutParams as CoordinatorLayout.LayoutParams
                params.gravity = Gravity.TOP
                view.layoutParams = params
    
                val layout           = customSnackBar.view as Snackbar.SnackbarLayout
                val customSnackView  = layoutInflater.inflate(R.layout.snackbar_banner, null)
    
                val actionButton1   = customSnackView.findViewById(R.id.action_button_1) as MaterialButton
                actionButton1.setOnClickListener {
                    customSnackBar.dismiss()
                }
                val actionButton   = customSnackView.findViewById(R.id.action_button_2) as MaterialButton
                actionButton.setOnClickListener {
                    customSnackBar.dismiss()
                }
    
                // We can also customize the above controls
                layout.setPadding(0, 0, 0, 0)
                layout.addView(customSnackView, 0)
    
    
                customSnackBar.show()
    

    XML:

    
    
    
        
    
        
    
            
    
            
    
        
    
    
    

提交回复
热议问题