我正在为安卓开发一种消息传递应用程序。当消息从数据库加载到自定义列表视图时,它们会显示为 OK。我希望它们像 android 的原生消息传递应用程序一样显示。我已经使用 9Patch 为发送者和接收者准备了两张图片。我想像这张图片一样显示http://s21.postimg.org/bj6idzdaf/example.png。但目前我有这个http://s23.postimg.org/805pfdxqz/example1.png。这是行代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chatLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/me"
android:orientation="vertical" >
<!-- User and CreatedAt -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="0dp" >
<TextView
android:id="@+id/textUser"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User"
android:textSize="15sp" />
<TextView
android:id="@+id/textCreatedAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Less than a minute"
android:textSize="10sp" />
</LinearLayout>
<!-- Message -->
<TextView
android:id="@+id/textText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:text="I'm text"
android:textSize="12sp" />
</LinearLayout>
这是布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listTimeline"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="#FFFFFF"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/messageText"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/sendTextBoxHint" />
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonSend" />
</LinearLayout>
</LinearLayout>
这是在列表视图中获取和显示消息的代码
class getAndDisplayMessages extends Thread {
@Override
public void run() {
while (!stopCheckingForMessages) {
try {
theApp.appData.openReadableDatabase();
cursor = theApp.appData.getMessages(user,
TheApplication.screenName);
Log.d(TAG, "cursor containing " + cursor.getCount()
+ " rows");
if (cursor == null) {
return;
}
startManagingCursor(cursor);
ChatActivity.this.runOnUiThread(new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
String[] FROM = { AppData.T_TIME,
AppData.SENDER_SCREEN_NAME,
AppData.MESSAGES };
int[] TO = { R.id.textCreatedAt, R.id.textUser,
R.id.textText };
adapter = new SimpleCursorAdapter(
ChatActivity.this, R.layout.chat_row,
cursor, FROM, TO);
adapter.setViewBinder(VIEW_BINDER);
listTimeline.setAdapter(adapter);
listTimeline.setSelection(adapter.getCount()-1);
}
});
Thread.sleep(DELAY);
} catch (Exception e) {
Log.e(TAG,
"Error Occured while checking for updated messages. \nErrorMessage:"
+ e.getMessage());
}
}
}
}