【发布时间】:2016-05-24 11:25:02
【问题描述】:
我使用列表视图在两个用户之间聊天。我想将接收者发送的聊天消息的背景颜色设置为蓝色,为发送者设置其他颜色。我还想将列表视图项目的大小限制为wrap_content,但它似乎不起作用,因此背景颜色遍布整个项目的宽度。
以下是包含列表视图的活动代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".ChatScreen">
<include layout="@layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<io.codetail.widget.RevealFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lMsgs"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/editText"
android:divider="@null"/>
<EditText
android:text=""
android:layout_width="wrap_content"
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/bSendMsg" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sendd"
android:id="@+id/bSendMsg"
android:layout_below="@+id/lMsgs"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<include layout="@layout/media_attach_menu" />
</io.codetail.widget.RevealFrameLayout>
</FrameLayout>
列表视图项的代码:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="left"
android:id="@+id/RelativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:text="Username"
android:id="@+id/tvUsername"
android:layout_alignParentTop="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvUsername"
android:id="@+id/innerRL">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_alignParentTop="false"
android:text="Message"
/>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"
android:contentDescription="@string/image"
android:scaleType="fitCenter"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:minHeight="50dp"
android:minWidth="50dp"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="14sp"
android:text="Timestamp"
android:id="@+id/tvTime"
android:layout_below="@id/innerRL"
/>
</RelativeLayout>
【问题讨论】: