简体   繁体   English

当我设置 ImageSpan 和 ellipsize 我想看到...和 ImageSpan 全部显示

[英]when I set ImageSpan and ellipsize I want see …and ImageSpan all display

I have a TextView and I want to achieve this ellipse effect:我有一个TextView ,我想实现这个椭圆效果:

这个

Here is my XML layout:这是我的 XML 布局:

<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我要TextView优先显示ImageSpan,然后计算文本宽度

I did not find a way to customize the internal layout of the TextView.我没有找到自定义 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我在两行的情况下计算了文本的长度,然后减去一个字符的长度,然后设置文本和ImageSpan

This is my code这是我的代码

                    TextPaint paint = songDetailTv.getPaint();
                    int textViewWidth = (int) (container.getResources().getDisplayMetrics().widthPixels
                            - (2 * Resource.getDimension(R.dimen.player_recommend_margin_horizontal))
                            - (2 * Resource.getDimension(R.dimen.player_recommend_root_view_margin)));
                    int bufferWidth = (int) paint.getTextSize();
                    int textLength = textViewWidth * 2 - bufferWidth;
                    paint.setTextSize(songDetailTv.getTextSize());
                    CharSequence temp = TextUtils.ellipsize(detail.podcastIntro, paint, textLength * 1f, TextUtils.TruncateAt.END);
                    boolean isEllipsize = temp == detail.podcastIntro;
                    if (!isEllipsize) {
                        songDetailTv.setText(SpannableHelper.getPicAtEnd(R.drawable.playing_com_into, temp, -1));
                    } else {
                        songDetailTv.setText(detail.podcastIntro);
                    }

this is xml,maxline=2这是xml,maxline=2

<TextView
        android:id="@+id/tv_song_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_song_publish_time"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:maxLines="2"
        android:textColor="#98999a"
        android:textSize="12dp"
        android:visibility="gone"
        android:layout_marginTop="15dp"
        tool:text="歌曲详情"
        tool:visibility="visible" />

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.这可能与TextView的布局有关。 But can tolerate但能忍

Due to the company's business, in addition to the need to add Image, it also needs to retain some special text, so I customized a TextView to complete this effect This is code link github由于公司的业务,除了需要添加Image外,还需要保留一些特殊的文字,所以我定制了一个TextView来完成这个效果这个是代码链接github

This is current effect这是当前效果

current effect当前效果

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

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