TextView spannableString 使用

val spannableString = SpannableString("在文本中添加表情(表情)")

val drawable =resources.getDrawable(R.drawable.msg,context?.getTheme())

// 设置ImageView 起始位置及宽高

drawable.setBounds(0,0,60,60)

val imageSpan = ImageSpan(drawable)

// 6-8间替换

spannableString.setSpan(imageSpan,6,8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

val textView = TextView(context)

textView.text = spannableString

val layoutParams = textView.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))

// 设置外边距

//layoutParams.setMargin()

// 设置内边距

//textView.setPadding(20, 0, 0, 0)

// 在"某父view"中添加文本

(this.requireView()as ViewGroup).addView(textView)

你可能感兴趣的:(TextView spannableString 使用)