我正在Android中创建一个聊天应用。 我能够使用以下列表项布局创建普通文本聊天列表视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/chat_msg_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/msg_send_time"
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_below="@+id/chat_msg_view"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="@android:color/holo_blue_dark" />
<TextView
android:id="@+id/msg_status"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/msg_send_time"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="@android:color/holo_blue_dark" />
</RelativeLayout>
但现在我正试图通过聊天发送图像。所以现在我需要将上面布局中的第一个TextView替换为ImageView,如果发送的内容是IMAGE。
这样做的好方法是什么。
答案 0 :(得分:0)
您可以使用许多方法。如果使用列表视图适配器,则可以使用itemType更改整个列表项布局(如果要更改整个布局,但可能有点复杂),或者可以将图像视图放置在布局中的相同位置作为文本视图并显示或隐藏您正在使用的任何一个。 I.E.如果显示图像,则显示图像视图并隐藏文本视图,如果显示文本视图,则显示文本并隐藏图像。然后,您不需要在运行时插入/删除视图,这有点复杂。
您的布局可能看起来有点像......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/chat_msg_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/chat_image_view"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:visibility="gone"/>
<TextView
android:id="@+id/msg_send_time"
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_below="@+id/chat_msg_view"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="@android:color/holo_blue_dark" />
<TextView
android:id="@+id/msg_status"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/msg_send_time"
android:layout_margin="2dp"
android:paddingTop="2dp"
android:textColor="@android:color/holo_blue_dark" />
</RelativeLayout>
注意chat_image_view如何也与父左边对齐..它将是在文本视图的顶部,但它的可见性“已消失”,所以你不会看到它。
希望这会有所帮助,祝你好运。