如何在单个xml drawable中添加多个形状

时间:2017-06-01 12:40:08

标签: android android-layout layout xml-drawable android-shapedrawable

我想使用xml资源创建聊天气泡,但我不能在右边缘的矩形外面制作三角形我也尝试使用9补丁图像但是当用户输入1个字符时仍然宽度不是包裹内容。请帮我,我也把xml资源的创建代码。

    ['Gear', 'REGION 27 SOUTHEAST', 'DISTRICT 7 E ATL', 'LOGANVILLE GA (6852)', 'WNDA3100 RANGEMAX', '32.99', '1']

enter image description here

2 个答案:

答案 0 :(得分:2)

查看这些库

它可以帮助你(Y)

Bubble 1

Bubble 2

Bubble 3

答案 1 :(得分:0)

以下是可绘制的custom_shape_chat_box.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Transparent Rectangle -->
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="60dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <!-- Colored Rectangle -->
    <item
        android:right="20dp">
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="60dp" />
            <solid android:color="#5EB888" />
        </shape>
    </item>

    <!-- Bottom-Right Triangle -->
    <item
        android:left="20dp"
        android:right="0dp"
        android:top="-10dp"
        android:bottom="20dp">
        <rotate android:fromDegrees="26">
            <shape android:shape="rectangle">
                <solid android:color="#5EB888" />
            </shape>
        </rotate>
    </item>

</layer-list>

使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_shape_chat_box">
    </LinearLayout>
</LinearLayout>

<强>输出:

enter image description here

希望这会有所帮助〜