使用“形状”或“9补丁图像”创建聊天气泡

时间:2015-02-13 18:19:05

标签: android shapes nine-patch

我正在尝试为我正在处理的Android应用中的聊天气泡创建模板。最终结果应如下所示:

enter image description here

我尝试使用Shapes,但我无法正确使用多个图层。我也尝试了一个9补丁图像,但创建9补丁是我得到的。我不知道如何使用它,特别是头像,邮件标题和内容展示位置。

任何人都可以帮忙吗?

我对形状的了解相当有限,不过,我想我知道的就是了解你们会说些什么:)

1 个答案:

答案 0 :(得分:6)

9补丁真的很容易。这是一个很好的教程:Simple Guide to 9 Patch 只需将扩展 .9.png ,而不仅仅是 .png
将其用作ViewGroup(您的View容器)的背景,如:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bubble_left"
    android:layout_margin="8dp"
    android:padding="8dp"
    >
    <!-- The User -->
    <TextView
        android:id="@+id/txtUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
    />
    <!-- The Date -->
    <TextView
        android:id="@+id/txtDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtUser"
    />
    <!-- The Message -->
    <TextView
        android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/txtDate"
    />
</RelativeLayout>

我让你自由选择你想要的图形(以符合你的品味而不是破坏你的乐趣)。

显然,你可能想要为左侧准备一个气泡,为右侧准备一个气泡(或者有不同颜色的气泡),并在Java代码中相应地交换它们。