android气泡聊天框大小,android – 备用聊天气泡宽度

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

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

>

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"

/>

android:layout_height="wrap_content" android:orientation="horizontal"

android:gravity="bottom" style="@android:style/ButtonBar">

android:id="@+id/stt_button"

android:layout_width="45dp"

android:layout_height="50dp"

android:background="@drawable/microphone"

/>

android:inputType="text"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1" />

android:layout_width="wrap_content"

android:layout_height="40dp"

android:layout_gravity="center_vertical"

android:text="@string/send_button" />

这是我的list_item布局:

android:orientation="horizontal"

android:layout_width="wrap_content" android:weightSum="1.0"

android:layout_weight="1" android:layout_height="wrap_content">

android:id="@+id/linearLayoutLeft"

android:layout_width="0dp"

android:layout_weight="0.8"

android:layout_height="wrap_content">

android:id="@+id/relativeLayoutLeft"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

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">

android:id="@+id/linearLayoutRight"

android:layout_width="0dp"

android:layout_weight="0.8"

android:layout_height="wrap_content">

android:id="@+id/relativeLayoutRight"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

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">

这是我的自定义数组适配器的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编程.问题是我的布局定义和代码过于复杂.现在我简化了我的布局层次结构,我已经实现了我想要的,没有使用任何布局权重和很多简单的代码/配置.在这里,我发布了更新的代码片段.

我的主要布局现在:

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="fill_parent">

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"/>

android:layout_height="wrap_content" android:orientation="horizontal"

android:gravity="bottom" style="@android:style/ButtonBar">

android:layout_width="45dp"

android:layout_height="50dp"

android:background="@drawable/microphone"/>

android:inputType="text"

android:layout_width="0dp"

android:layout_height="40dp"

android:layout_weight="1" />

android:layout_width="wrap_content"

android:layout_height="40dp"

android:layout_gravity="center_vertical"

android:text="@string/send_button" />

列表项布局:

android:layout_width="wrap_content" android:weightSum="1.0"

android:layout_weight="1" android:layout_height="wrap_content">

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"/>

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"/>

这是我的自定义数组适配器的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-layout

来源: https://codeday.me/bug/20190902/1791497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值