代码之家  ›  专栏  ›  技术社区  ›  efuntikov

android将聊天气泡宽度调整为包装文本

  •  1
  • efuntikov  · 技术社区  · 7 年前

    image

    对ViewHierarchy的调查表明,这些间隙位于聊天布局的TextView、view层次结构内。所以它与九补丁无关。 image

    可能有人面临类似的问题。。。

    <LinearLayout
    android:id="@+id/messages"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/senderName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:gravity="bottom|start"
        android:maxLines="1"
        android:layout_marginTop="5dp"
        android:layout_marginStart="18dp"
        android:layout_marginLeft="18dp"
        android:textColor="@color/talkatone_gray"
        android:textSize="@dimen/chat_item_meta_info_text_size"
        android:visibility="gone"/>
    
        <TextView
            android:id="@+id/message_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginBottom="@dimen/chat_bubble_margin_top"
            android:background="@android:color/transparent"
            android:lineSpacingExtra="4sp"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:text="@string/form_chat_failed_to_open"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/chat_text"
            android:textSize="@dimen/chat_text_message_size"/>
    
        <FrameLayout
            android:id="@+id/attachment_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/chat_bubble_margin_top"
            android:orientation="vertical">
    
                <ImageButton
                    android:id="@+id/video_play_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:background="@android:color/transparent"
                    android:contentDescription="@null"
                    android:src="@drawable/play_gif"/>
        </FrameLayout>
    

    3 回复  |  直到 7 年前
        1
  •  1
  •   v1k    7 年前

    在您的案例中,主要问题是在adapter with viewholder模式实现中使用它。

    因此,有两个问题需要解决:

    对于reset TextView width add to your adapter getView()方法,当您重新使用支架时,接下来:

    ViewGroup.LayoutParams paramsReset = holder.messageText.getLayoutParams();
    paramsReset.width = ViewGroup.LayoutParams.WRAP_CONTENT;
    holder.messageText.setLayoutParams(paramsReset);
    

    对于第二个问题,您需要创建自定义视图,从TextView扩展并重写onDraw()方法:

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if(getLineCount() > 1)
        {
            float longestLineWidth = -1;
            for (int lineIndex = 0; lineIndex < getLineCount(); lineIndex++)
            {
                int lineStartIndex = getLayout().getLineStart(lineIndex);
                int lineEndIndex = getLayout().getLineEnd(lineIndex);
                String currentTextLine = getText().toString().substring(lineStartIndex, lineEndIndex);
                // Added "_____" for your paddings.
                float currentLineWidth = getPaint().measureText(currentTextLine + "_____");
    
                if (longestLineWidth < currentLineWidth)
                {
                   longestLineWidth = currentLineWidth;
                }
            }
            ViewGroup.LayoutParams paramsNew = getLayoutParams();
            paramsNew.width = (int) longestLineWidth;
            setLayoutParams(paramsNew);
        }
    }
    

    我重写onDraw()也是因为需要在ListView中使用它,看起来您需要找到其他回调,当holder出现在视图中时调用。


    它可以工作,但效率不高,还有一些其他问题,请将其作为第一步。

        2
  •  0
  •   Basavannevva    7 年前

    使用9个面片图像作为布局的背景,可以创建位图图像,自动调整大小以适应视图内容。

        3
  •  0
  •   Ben P.    7 年前

    不幸的是,您的问题还包括您的答案:

    当您使用 wrap_content 并允许多行,系统将首先增加 TextView 尽可能宽,然后才开始添加换行符。它从不在最后传递一次以回收可用宽度。

    当然,正如所有事情一样,你可以通过编写自己的习惯来解决这个问题 文本框 班这是另一个试图解决类似问题的项目的链接;也许这会给你一些想法。

    其他问题: Uniform text wrapping in TextView

    https://github.com/dziobas/UniformTextView