我正在为此开发一个聊天应用程序,我正在使用 9 补丁泡泡
在屏幕上显示气泡的功能
static void showMessage(String message, boolean leftSide)
{
Log.d("abc","show");
final TextView textView = new TextView(activity);
textView.setTextColor(Color.BLACK);
textView.setText(message);
int bgRes = R.drawable.left_message_bg;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (!leftSide)
{
bgRes = R.drawable.right_message_bg;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(params);
textView.setBackgroundResource(bgRes);
messagesContainer.addView(textView);
// Scroll to bottom
if (scrollContainer.getChildAt(0) != null)
{
scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
XML 布局
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/messageEdit"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:autoText="true"
android:hint="message text"
android:layout_alignParentRight="false"
android:layout_toLeftOf="@+id/sendButton"
android:layout_marginRight="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/sendButton"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<ScrollView
android:id="@+id/scrollContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_marginBottom="20dp" android:layout_above="@+id/messageEdit"
android:layout_below="@+id/meLabel" android:layout_marginTop="10dp">
<LinearLayout
android:id="@+id/messagesContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/headerforchat"
android:text="Me"
android:id="@+id/meLabel" android:layout_gravity="left|center_vertical" android:singleLine="false"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/headerforchat"
android:text="Friend"
android:id="@+id/friendLabel" android:layout_alignParentRight="true" android:textSize="20dp"/>
</RelativeLayout>
但是在 android 2.3 上测试时会出现奇怪的行为。它在三星 Glaxy S3 中看起来不错
第一张是 Galaxy S3 截图。第二个 Android 2.3 屏幕截图。蓝色气泡在 android 2.3 中显示很奇怪
left_message_bg-->The orange bubble which will display after sending message
right_message_bg-->The blue bubble which will display after receiving message
9 个补丁图像
任何帮助将不胜感激