- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个聊天类型的应用程序。我正在使用两个不同的 9-patch 来处理聊天气泡主要消息和响应。我面临的问题是根据消息长度自动包装气泡的宽度。以下是我的主要布局:
<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" style="@android:style/ButtonBar">
<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,因此无论 wrap_content 是什么,右边的气泡总是 0.8。我怎样才能解决这个问题?我想要的只是根据文本宽度获取气泡并左对齐或右对齐。如果您能建议一种更好的方法来正确执行此操作,我可以完全放弃我目前的方法。
最佳答案
当我第一次发布这个问题时,我才刚刚开始 Android 编程。问题是我的布局定义和代码过于复杂。现在我简化了我的布局层次结构,我已经实现了我想要的并且没有使用任何布局权重并且有很多简单的代码/配置。我在这里发布我更新的代码 fragment 。
我现在的主要布局:
<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" style="@android:style/ButtonBar">
<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 - 备用聊天气泡宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9101835/
我想知道 gmail 聊天如何允许用户连接到 AIM,然后像登录到 AIM 一样聊天。 做起来容易吗?怎么做到的? 有人知道任何类似的开源工具吗? 谢谢! 最佳答案 如果你在谈论编程,这里是源代码示例
大家好,我正在尝试制作一个游戏,两个主持人联系起来,他们将“掷硬币”,并确定谁先出局。我决定从基本代码开始。但是我真的没有主意。 Thread server2 = new Thread(new Ser
我已经创建了一个只有 1 个房间的聊天室、私有(private)消息、审核以及一切,现在一切都很好!当我测试聊天时,我意识到在聊天中输入的所有消息都会被保存,如果有很多人使用聊天,它很快就会占用 Fi
当用户键入内容并出现软键盘时,我必须保持聊天回收器 View 的当前项目可见。目前,它覆盖了聊天,我需要回收器 View 项目与键盘一起显示。 我在 list 中尝试了这些: -android:win
我有一个服务器客户端应用程序集。 (家庭作业) 到目前为止,我已经弄清楚如何让多个客户端连接到服务器并让服务器聚合客户端发送的消息,以及如何让服务器将客户端的消息发送回客户端并将其显示在聊天 Pane
如何从我的应用程序发送/接收 Facebook 聊天消息?它是用 .Net、C# 编写的。 最佳答案 如果你可以使用 C,你就可以使用 libpurple (GPL) 和 pidgin-faceboo
我正在使用启用的 Ajax-Wcf 服务开发 Asp.Net 聊天。这是一个非常简单的聊天引擎,其中消息对话框意味着一对一(单个用户),但是我不知道如何管理(以最佳方式)通知新消息可用性。例如,假设有
我的任务是通过服务器构建一个客户端到客户端的聊天程序。客户端 A 将向服务器发送一条消息,然后服务器将消息转发给客户端 B,反之亦然。所有这一切都将同时发生,直到其中一个将其关闭。我有以下程序。 服务
我创建了一个聊天,用户可以在其中输入文本的输入字段。当他输入文本并按下发送(或输入)时,文本位于输入字段上方。像这样: 我想要的:我希望输入字段位于页面底部。我使用 position: absolut
出于个人兴趣,我尝试定义一个模拟 AI,它基于他学到的信息和互联网搜索,以便提供比系统知道的更多的细节。 我举了一个 child 的例子,当他出生时他需要学习一切,他听到了很多然后提出了一些答案。他的
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
我已经开始聊天了,但我已经将用户的 ID 硬编码到 Chat.php 中。 当他们登录站点时,我的登录名将他们的电子邮件设置为 session ( $_SESSION['email']=$email;
当用户点击像 Start a viber chat with us 这样的链接时,我试图找到一种方法来开始 viber 聊天。但到目前为止我没有找到正确的URI来做到这一点。例如,我知道我可以使用 s
我是 Javascript(纯 javascript)新手,我正在尝试创建一个执行以下操作的聊天 Controller 应用程序。 用户输入内容。 有人对我的知识库进行了后调用。 服务器响应消息。 目
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
如果用户在 x 秒/分钟内处于非事件状态,我想结束聊天,以便我们的代理不必等待聊天自行关闭。我还想在结束聊天之前将标签附加到聊天中,以便我可以看到这是由于不活动造成的。 最佳答案 此内容归功于 j
我正在此网站中构建新网站,客户需要 24/7 实时客户支持。我想在网站上集成 Skype 聊天 聊天界面应该在客户端的网站上。 最佳答案 您可以通过在网站上放置 Skype 按钮来使用它。 http:
事实上,我只是开始积极练习 swing,以便我的理论知识能派上用场:) 我已经为聊天 GUI 实现做了很多工作,但最终遇到了一些问题。所以我决定从头开始重新设计聊天 GUI,但我需要为其选择正确的组件
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
我正在尝试进行简单的聊天,其中连接到服务器的用户发送消息,其他用户接收消息。 这是我的 html: function setupEventSource()
我是一名优秀的程序员,十分优秀!