当我尝试删除小吃栏的父布局并使用相同布局重新创建小吃栏时,textView 的值显示为空白。
我也尝试过调试相同的内容,在使用调试器时我得到了文本字符串,但在运行时它没有显示在小吃栏中。
这是我的自定义 SnackBar 的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/dr_toast_round_corners_10dp_red"
android:id="@+id/toast_container">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_error_red"
android:padding="@dimen/dimen_4dp"
android:layout_marginStart="@dimen/dimen_16dp"
android:layout_marginVertical="@dimen/dimen_12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_invalid_credentials"
android:layout_width="@dimen/dimen_0dp"
android:layout_height="wrap_content"
android:paddingHorizontal="@dimen/dimen_16dp"
android:fontFamily="@font/inter_regular"
tools:text="@string/invalid_credentials_login"
android:textColor="@color/text_body_light"
android:textSize="@dimen/dimen_13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_close"
app:layout_constraintStart_toEndOf="@+id/iv_error"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_close"
android:padding="@dimen/dimen_4dp"
android:layout_marginEnd="@dimen/dimen_24dp"
android:layout_marginVertical="@dimen/dimen_12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是 SnackBar 的代码:
fun showCustomErrorSnackbar(view: ViewGroup, activity: Activity, message: String) {
snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE)
snackbar.duration = 2000
val customLayout =
activity.layoutInflater.inflate(
R.layout.layout_custom_toast,
activity.findViewById(R.id.toast_container)
)
snackbar.view.setBackgroundColor(Color.TRANSPARENT)
val textview = customLayout.findViewById<AppCompatTextView>(R.id.tv_invalid_credentials)
textview.text = message
// now change the layout of the snackbar
val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout
snackbarLayout.removeAllViewsInLayout()
// set padding of the all corners as 0
customLayout.setPadding(0, 0, 0, 0)
val layoutParameters = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
layoutParameters.gravity = Gravity.CENTER_HORIZONTAL
layoutParameters.setMargins(24, 16, 24, 0) //left = 24, top = 16, right = 24, bottom = 0
snackbarLayout.layoutParams = layoutParameters
val closeSnackbar = customLayout.findViewById<AppCompatImageView>(R.id.iv_close)
closeSnackbar.setOnClickListener {
snackbar.dismiss()
}
val parent = customLayout.parent as? ViewGroup
parent?.removeView(customLayout)
snackbarLayout.addView(customLayout, 0)
snackbar.show()
}
第一次以这样的方式显示零食栏:
但是很快就显示成这样了:
有人可以帮我解决这个问题吗?
我希望小吃栏能够完美运行,而不会跳过文本或布局。另外,我想关闭十字按钮点击侦听器处的小吃栏。
我已经尝试过您分享的代码片段。进行了一些重构,它对我来说非常有效。下面是对我有用的代码。
fun showCustomErrorSnackbar(
view: View,
message: String,
isFragmentView: Boolean
) {
if (!::customLayout.isInitialized) {
customLayout = layoutInflater.inflate(
R.layout.layout_custom_toast,
requireActivity().findViewById(R.id.toast_container)
)
}
val snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE).apply { duration = 1500 }
(customLayout.parent as? ViewGroup)?.removeView(customLayout)
snackbar.view.setBackgroundColor(Color.TRANSPARENT)
customLayout.findViewById<AppCompatTextView>(R.id.tv_invalid_credentials).text = message
customLayout.findViewById<AppCompatImageView>(R.id.iv_close)
.setOnClickListener { snackbar.dismiss() }
val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout
snackbarLayout.setPadding(0, 0, 0, 0)
(snackbarLayout.layoutParams as FrameLayout.LayoutParams).apply {
width = ViewGroup.LayoutParams.MATCH_PARENT
gravity = Gravity.TOP
}
val layoutParameters = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
).apply {
gravity = Gravity.CENTER_HORIZONTAL
setMargins(24, if (isFragmentView) 16 else 128, 24, 0)
}
customLayout.layoutParams = layoutParameters
snackbarLayout.addView(customLayout, 0)
snackbar.show()
}
您的代码中只需要进行少量重构,而不是在显示小吃栏之前删除底部的视图。使用
Snackbar.make()
方法初始化小吃栏后,尝试将其从顶部删除。