繁体   English   中英

如何在 TextView Android 附近下车(或自定义 snackbar android)

[英]How to alight drawable left near to TextView Android (Or custom snackbar android)

我正在为 android 使用自定义 SnackBar,我创建了带有图标和消息的 SnackBar。 可绘制图标未按预期工作。 页边距文本比预期的要远。 所以,这就是我所做的: SnackBar-1

预期的自定义小吃店是: SnackBar-2我怎样才能得到我预期的 output? 非常感谢你。

这是我的代码:

fun showSuccessFullMsg(message: String, anchorView: View) {
    val snackBarView = Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG)
    val view = snackBarView.view
    val params = view.layoutParams as FrameLayout.LayoutParams
    snackBarView.setBackgroundTint(resources.getColor(android.R.color.white))
    params.gravity = Gravity.TOP
    params.gravity = Gravity.CENTER_HORIZONTAL
    val actionBarHeight = anchorView.height

    params.setMargins(32, actionBarHeight.plus(36), 32, 0)
    view.layoutParams = params

    val textView = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
    textView.textAlignment = View.TEXT_ALIGNMENT_CENTER
    val drawable = resources.getDrawable(
        R.drawable.ic_double_check,
        resources.newTheme()
    )
    (textView.layoutParams as LinearLayout.LayoutParams).gravity = Gravity.CENTER_HORIZONTAL
    drawable.bounds = Rect(0, 0, 32, 24)
    textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null)
    textView.isSingleLine = true
    textView.ellipsize = TextUtils.TruncateAt.END
    view.background =
        ContextCompat.getDrawable(
            this,
            com.proxglobal.ringtone.R.drawable.bg_snackbar
        ) // for custom background
    snackBarView.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
    snackBarView.setTextColor(Color.parseColor("#23C16B"))
    snackBarView.show()
}

删除线

textView.textAlignment = View.TEXT_ALIGNMENT_CENTER

并以编程方式将填充添加到TextView的开头。 您必须根据TextView 、可绘制对象和文本的大小进行计算,以确定要应用多少填充。

第二种方法是使用ImageSpan将可绘制对象添加到文本本身。

感谢@Cheticamp 的回答。 最后,这是我的代码:

  @SuppressLint("PrivateResource", "UseCompatLoadingForDrawables")
fun showSuccessFullMsg(message: String, anchorView: View) {
    val drawable = resources.getDrawable(
        R.drawable.ic_double_check,
        resources.newTheme()
    )
    drawable.bounds = Rect(0, 0, 64, 48)
    val imageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
    val sp = SpannableString("  $message")
    sp.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
    val snackBarView = Snackbar.make(binding.root, sp, Snackbar.LENGTH_LONG)
    val view = snackBarView.view
    val params = view.layoutParams as FrameLayout.LayoutParams
    snackBarView.setBackgroundTint(resources.getColor(android.R.color.white))
    params.gravity = Gravity.TOP
    params.gravity = Gravity.CENTER_HORIZONTAL
    val actionBarHeight = anchorView.height

    params.setMargins(32, actionBarHeight.plus(36), 32, 0)
    view.layoutParams = params

    val textView = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
    textView.textAlignment = View.TEXT_ALIGNMENT_CENTER

    (textView.layoutParams as LinearLayout.LayoutParams).gravity = Gravity.CENTER_HORIZONTAL
    textView.isSingleLine = true
    textView.ellipsize = TextUtils.TruncateAt.END
    view.background =
        ContextCompat.getDrawable(
            this,
            R.drawable.bg_snackbar
        ) // for custom background
    snackBarView.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
    snackBarView.setTextColor(Color.parseColor("#23C16B"))
    snackBarView.show()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM