使用.png文件中的形状创建xml聊天气泡

时间:2017-01-26 14:11:02

标签: android xml chat shape balloon

我可以问一下是否有人知道如何在下面创建一个xml形状的聊天气泡?我在网上找到的唯一例子是两个独立的形状,在布局中作为两个单独的背景放置 - 三角形和矩形。我尝试将三角形和矩形结合起来无济于事。三角形似乎隐藏在矩形中。 将top属性设置为三角形以将其向下移动会使ImageView对象变为空白。

请看看我尝试了什么:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--rectangle with rounded corners-->
    <item android:top="0dp" android:bottom="20dp">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@android:color/black" />
            <size
                android:width="106dp"
                android:height="20dp"/>
            <stroke
                android:width="1dp"
                android:color="#000" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <!--triangle-->
    <item android:bottom="0dp" android:top="20dp" android:gravity="center_horizontal|bottom">
        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="135%"
            android:pivotY="15%">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/black" />
                <size
                    android:height="10dp"
                    android:width="10dp"/>
            </shape>
        </rotate>
    </item>
</layer-list>

这是imageView中的用法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.eventilate.shapes.Balloon">
    <ImageView
        android:layout_width="106dp"
        android:layout_height="30dp"
        android:id="@+id/imageView2"
        android:contentDescription="@string/test"
        android:background="@drawable/balloon"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以使用此代码

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

<item android:top="0dp" android:bottom="10dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@android:color/holo_blue_light" />
        <size
            android:width="106dp"
            android:height="20dp"/>
        <stroke
            android:width="1dp"
            android:color="#40adc2" />
        <corners android:radius="4dp" />
    </shape>
</item>

<item android:bottom="0dp"
    android:top="0dp"
    android:gravity="center_horizontal|bottom">
    <rotate
        android:fromDegrees="45"
        android:toDegrees="45"
        android:pivotX="135%"
        android:pivotY="15%">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/holo_blue_light" />
            <size
                android:height="10dp"
                android:width="10dp"/>
        </shape>
    </rotate>
</item>
</layer-list>

它得到了这个结果: enter image description here

答案 1 :(得分:1)

这正是您所需要的:

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

    <!-- red shape -->
    <item
        android:bottom="0dp"
        android:gravity="center_horizontal|bottom"
        android:top="0dp">
        <rotate
            android:fromDegrees="45"
            android:pivotX="110%"
            android:pivotY="17%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/holo_red_dark" />
                <size
                    android:width="10dp"
                    android:height="10dp" />
            </shape>
        </rotate>
    </item>

    <item
        android:bottom="10dp"
        android:top="0dp">
        <shape>
            <corners android:radius="3dp" />
            <solid android:color="@android:color/holo_red_dark" />
            <size
                android:width="50dp"
                android:height="20dp" />
        </shape>
    </item>

    <!-- black shape -->
    <item
        android:bottom="0dp"
        android:gravity="center_horizontal|bottom"
        android:top="0dp">

        <rotate
            android:fromDegrees="45"
            android:pivotX="120%"
            android:pivotY="15%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/black" />
                <size
                    android:width="10dp"
                    android:height="10dp" />
            </shape>
        </rotate>
    </item>

    <item
        android:bottom="10.5dp"
        android:left="0.5dp"
        android:right="0.5dp"
        android:top="0.5dp">
        <shape>
            <corners android:radius="3dp" />
            <solid android:color="@android:color/black" />
            <size
                android:width="50dp"
                android:height="20dp" />
        </shape>
    </item>

</layer-list>

enter image description here