最近公司有一个关于snackbar的需求,就是可以自定义snackbar显示在屏幕的位置,在网上搜了很多关于怎么让snackbar显示在距离屏幕底部一定距离的文章,但是基本就是不行。结合了两个其他人的想法,终于搞定了。可以参照其他人的链接:snackbar 自定义
snackbar 的bottomMargin
实现代码:
一个自定义snackbar 的工具类
class SnackbarUtil {
companion object {
fun showSnackbar(view: View, message: String, bottomMargin: Int, isAddAnchrview: Boolean, isShowAction: Boolean, actionText: String, snackbarActionListener: SnackbarActionListener?) {
val snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT).addCallback(
object : Snackbar.Callback() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
super.onDismissed(transientBottomBar, event)
snackbarActionListener?.onSnackbarDismissed(event)
}
}
)
val snackbarView = snackbar.view
if (isAddAnchrview) {
//这个属性是决定于snackbar显示在哪个控件上
snackbar.anchorView = view
}
val snackbarLayout = snackbarView as Snackbar.SnackbarLayout
snackbarLayout.setBackgroundColor(Color.TRANSPARENT)
val textView = snackbarLayout.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
textView.visibility = View.INVISIBLE
val customView = LayoutInflater.from(view.context).inflate(R.layout.layout_snackbar_custom_text, LinearLayout(view.context), false) as ViewGroup
val childLayout = customView.getChildAt(0)
val params = childLayout.layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin + bottomMargin)
childLayout.layoutParams = params
customView.findViewById<AppCompatTextView>(R.id.tv_action).apply {
if (isShowAction) {
text = actionText
visibility = View.VISIBLE
this.setOnClickListener {
snackbarActionListener?.onSnackbarClicked()
}
} else {
visibility = View.GONE
}
}
customView.findViewById<AppCompatTextView>(R.id.tv_snackbar).text = message
snackbarLayout.addView(customView, 0)
snackbar.show()
}
}
interface SnackbarActionListener {
fun onSnackbarClicked()
fun onSnackbarDismissed(event: Int)
}
}
//自定义layout, 这里需要说明的是必须要有两层布局,否则就会有bug
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_snackbar_color"
android:minHeight="48dp"
android:orientation="horizontal"
android:elevation="6dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_snackbar"
style="@style/Body"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
android:textColor="@color/white"
tools:text="Snack bar message goes here, when there are ." />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_action"
style="@style/Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/indigo_400"
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:layout_gravity="center"
android:text="Action" />
</LinearLayout>
</LinearLayout>
最后在activity或者fragment实现
context?.resources?.getDimensionPixelSize(R.dimen.dp_18)?.let {
SnackbarUtil.showSnackbar(mView,
"Snack bar message goes here, when there are two lines of.", it, isAddAnchrview = true,
isShowAction = false, actionText = "", snackbarActionListener = null)
}
来源:CSDN
作者:qq_32726703
链接:https://blog.csdn.net/qq_32726703/article/details/103460190