android– 备用聊天气泡宽度

android– 备用聊天气泡宽度,第1张

概述我正在开发一个聊天类型的应用程序我正在使用两个不同的九个补丁来聊天泡泡主要消息和响应.我面临的问题是根据消息长度自动包装气泡宽度.以下是我的主要布局:    <RelativeLayoutandroid:orientation="vertical"android:layout_width="wrap_content"androi

我正在开发一个聊天类型的应用程序我正在使用两个不同的九个补丁来聊天泡泡主要消息和响应.我面临的问题是根据消息长度自动包装气泡的宽度.以下是我的主要布局:
    

<relativeLayout    androID:orIEntation="vertical"    androID:layout_wIDth="wrap_content"    androID:layout_height="0dp"    androID:layout_weight="1"    > <ListVIEw androID:ID="@+ID/List"    androID:layout_wIDth="wrap_content"    androID:layout_height="fill_parent"    androID:stackFromBottom="true"    androID:transcriptMode="alwaysScroll"    androID:cachecolorHint="#00000000"    androID:ListSelector="@androID:color/transparent"    androID:divIDer="@null"    /> </relativeLayout><linearLayout androID:ID="@+ID/footer" androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content" androID:orIEntation="horizontal"    androID:gravity="bottom" >    <button        androID:ID="@+ID/stt_button"        androID:layout_wIDth="45dp"        androID:layout_height="50dp"        androID:background="@drawable/microphone"    />       <EditText androID:ID="@+ID/chat_msg"        androID:inputType="text"         androID:layout_wIDth="0dp"        androID:layout_height="40dp"        androID:layout_weight="1" />    <button androID:ID="@+ID/send_button"        androID:layout_wIDth="wrap_content"        androID:layout_height="40dp"        androID:layout_gravity="center_vertical"        androID:text="@string/send_button" /></linearLayout></linearLayout>

这是我的List_item布局:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:orIEntation="horizontal"androID:layout_wIDth="wrap_content" androID:weightSum="1.0"androID:layout_weight="1" androID:layout_height="wrap_content"><linearLayout             androID:ID="@+ID/linearLayoutleft"     androID:layout_wIDth="0dp"    androID:layout_weight="0.8"    androID:layout_height="wrap_content">    <relativeLayout         androID:ID="@+ID/relativeLayoutleft"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content">        <TextVIEw             androID:ID="@+ID/lefttext"             androID:layout_wIDth="wrap_content"             androID:gravity="top"             androID:layout_height="wrap_content"             androID:paddingleft="10dip"             androID:paddingRight="10dip"             androID:paddingtop="5dip"             androID:layout_alignParentleft="true">        </TextVIEw>    </relativeLayout></linearLayout><linearLayout             androID:ID="@+ID/linearLayoutRight"    androID:layout_wIDth="0dp"             androID:layout_weight="0.8"    androID:layout_height="wrap_content">    <relativeLayout         androID:ID="@+ID/relativeLayoutRight"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content">        <TextVIEw             androID:ID="@+ID/righttext"             androID:layout_wIDth="wrap_content"             androID:layout_height="wrap_content"             androID:paddingleft="10dip"             androID:paddingRight="10dip"             androID:paddingtop="5dip"             androID:layout_alignParentRight="true"             androID:layout_alignParenttop="true">        </TextVIEw>    </relativeLayout></linearLayout>

这是我的自定义数组适配器的getVIEw方法中的代码:

    VIEw vIEw = convertVIEw;    if(vIEw == null){        vIEw = mInflater.inflate(R.layout.List_item, null);    }    Resources res = getContext().getResources();    Drawable bubblesChat = res.getDrawable(R.drawable.bubbles_chat);    Drawable bubblesResponse = res.getDrawable(R.drawable.bubbles_response);    TextVIEw left = (TextVIEw) vIEw.findVIEwByID(R.ID.lefttext);    TextVIEw right = (TextVIEw) vIEw.findVIEwByID(R.ID.righttext);    VIEw leftLayout = vIEw.findVIEwByID(R.ID.relativeLayoutleft);    VIEw rightLayout = vIEw.findVIEwByID(R.ID.relativeLayoutRight);     linearLayout linearleft = (linearLayout) vIEw.findVIEwByID(R.ID.linearLayoutleft);    linearLayout linearRight = (linearLayout) vIEw.findVIEwByID(R.ID.linearLayoutRight);    LayoutParams leftParams = (LayoutParams) linearleft.getLayoutParams();    LayoutParams rightParams = (LayoutParams) linearRight.getLayoutParams();    String txt = super.getItem(position);    if(txt.startsWith("s:")) {        left.setText(getItem(position));        leftLayout.setBackgroundDrawable(bubblesChat);        leftParams.weight = 0.8f;        linearleft.setLayoutParams(leftParams);        rightParams.weight = 0.2f;        linearRight.setLayoutParams(rightParams);        right.setText("");        rightLayout.setBackgroundDrawable(null);    } else {        right.setText(getItem(position));        rightLayout.setBackgroundDrawable(bubblesResponse);        rightParams.weight = 0.8f;        linearRight.setLayoutParams(rightParams);                   leftParams.weight = 0.2f;        linearleft.setLayoutParams(leftParams);        left.setText("");        leftLayout.setBackgroundDrawable(null);    }    return vIEw;

我从这个设置得到的所有内容如下(注意右边气泡前面的空白区域:

您可以看到右侧气泡未根据文本大小包裹宽度.我理解为什么会发生这种情况 – 我将权重0.8分配给当前的聊天消息泡泡(可能是左边的),其余的是0.2.当前消息来自左侧气泡时,它可以正常工作,因为它首先使用wrap_content绘制为0.8重量.但是当正确的气泡是当前消息时,左边的气泡首先被绘制并且具有0.2的固定宽度,因此正确的气泡总是得到0.8而不管wrap_content.我怎样才能解决这个问题?我想要的只是根据文字宽度获取气泡并向左或向右对齐.如果你能提出一个更好的方法来正确地做到这一点,我可以完全抛弃我目前的方式.

解决方法:

当我第一次发布这个问题时,我刚开始进行AndroID编程.问题是我的布局定义和代码过于复杂.现在我简化了我的布局层次结构,我已经实现了我想要的,没有使用任何布局权重和很多简单的代码/配置.在这里,我发布了更新的代码片段.

我的主要布局现在:

 <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical"    androID:layout_wIDth="wrap_content"    androID:layout_height="fill_parent">    <ListVIEw androID:ID="@+ID/List"        androID:layout_wIDth="fill_parent"        androID:layout_height="0dip"        androID:layout_weight="1"        androID:stackFromBottom="true"        androID:transcriptMode="alwaysScroll"        androID:cachecolorHint="#00000000"        androID:ListSelector="@androID:color/transparent"        androID:divIDer="@null"/>    <linearLayout androID:ID="@+ID/footer" androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" androID:orIEntation="horizontal"        androID:gravity="bottom" >        <button androID:ID="@+ID/stt_button"            androID:layout_wIDth="45dp"            androID:layout_height="50dp"            androID:background="@drawable/microphone"/>           <EditText androID:ID="@+ID/chat_msg"            androID:inputType="text"             androID:layout_wIDth="0dp"            androID:layout_height="40dp"            androID:layout_weight="1" />        <button androID:ID="@+ID/send_button"            androID:layout_wIDth="wrap_content"            androID:layout_height="40dp"            androID:layout_gravity="center_vertical"            androID:text="@string/send_button" />    </linearLayout></linearLayout>

列表项布局:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content" androID:weightSum="1.0"    androID:layout_weight="1" androID:layout_height="wrap_content">    <TextVIEw androID:ID="@+ID/lefttext"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content"         androID:layout_marginleft="10dip"         androID:layout_marginRight="10dip"         androID:layout_margintop="10dip"        androID:layout_marginBottom="5dip"        androID:layout_alignParentleft="true"        androID:maxWIDth="250dip"/>    <TextVIEw         androID:ID="@+ID/righttext"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content"        androID:layout_marginleft="10dip"         androID:layout_marginRight="10dip"         androID:layout_margintop="10dip"        androID:layout_marginBottom="5dip"        androID:layout_alignParentRight="true"        androID:maxWIDth="250dip"/></relativeLayout>

这是我的自定义数组适配器的getVIEw方法中的代码:

    VIEw vIEw = convertVIEw;    if(vIEw == null){         vIEw = mInflater.inflate(R.layout.List_item, null);    }    Resources res = getContext().getResources();    Drawable bubblesChat = res.getDrawable(R.drawable.bubbles_chat);    Drawable bubblesResponse = res.getDrawable(R.drawable.bubbles_response);    TextVIEw left = (TextVIEw) vIEw.findVIEwByID(R.ID.lefttext);    TextVIEw right = (TextVIEw) vIEw.findVIEwByID(R.ID.righttext);    String txt = super.getItem(position);    if(txt.startsWith("s:")) {        left.setText(getItem(position));        left.setBackgroundDrawable(bubblesChat);        right.setText("");        right.setBackgroundDrawable(null);    } else {        right.setText(getItem(position));        right.setBackgroundDrawable(bubblesResponse);        left.setText("");        left.setBackgroundDrawable(null);    }    return vIEw;
总结

以上是内存溢出为你收集整理的android – 备用聊天气泡宽度全部内容,希望文章能够帮你解决android – 备用聊天气泡宽度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105702.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存