I have a TextView
and I want to achieve this ellipse effect:
Here is my XML layout:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="@color/white"
android:ellipsize="end"
android:maxLines="2"
/>
My code:
val drawable = ContextCompat.getDrawable(requireSceneContext() , R.drawable.playing_com_into)!!
drawable.setBounds(0, 0 , dp2px(10) , dp2px(10))
val text = "电影《We are all human》主题曲,电影《We are all human》主题曲,电影《We are all human》主题曲,电影《We are all human》主题曲,电影《We are all human》主题曲,电影《We are all human》主题曲"
val span = QMUIAlignMiddleImageSpan(drawable , ALIGN_MIDDLE , -1f)
val spannableString = SpannableString(text)
spannableString.setSpan(span , text.length -2 , text.length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE )
binding.tv1.text = spannableString
current effect
Here is what I'm getting right now:
I want TextView priority display ImageSpan, After that calculation Text width
I did not find a way to customize the internal layout of the TextView. I calculated the length of the text in the case of two lines, and then subtracted the length of one character, and then set the text and ImageSpan
This is my code
this is xml,maxline=2
this is current effect current effect
I don’t know why the calculation is sometimes not so accurate. This may have something to do with the layout of the TextView. But can tolerate