如何将气泡添加到textview android?

问题描述 投票:17回答:2

在我的应用程序中,我想将气泡设置为文本视图,在文本视图中我添加setBackgroundResource(),如您在代码中看到的那样。

使用此代码我得到这样的图像:

我想要一个像这样的气泡形状图像:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
    <solid android:color="#EDF5E4" />
    <corners android:bottomLeftRadius="@dimen/corner_radius"
    android:bottomRightRadius="@dimen/corner_radius"
    android:topLeftRadius="@dimen/corner_radius"
    id:topRightRadius="@dimen/corner_radius" />

请告诉我如何在我的setBackgroundResource() XML中进行此操作。

android android-intent android-custom-view nine-patch
2个回答
31
投票

您需要使用的是一个9补丁图像(正如Ken Wolf在本评论中已经指出的那样)。

为了帮助您入门,我在其中一个应用程序中包含了一组9个补丁图像,以及在创建布局XMl时如何使用它的简短代码。 ;-)

9补丁图像集:

(分别命名为:bubble_white_normal_mdpi.9bubble_white_normal_hdpi.9bubble_white_normal_xhdpi.9。将文件名放入各自的drawable文件夹后,从文件名中删除_mdpi,_hdpi和_xhdpi。

XML:

<LinearLayout
    android:id="@+id/linlaUserOther"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:padding="2dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="top|center" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imgvwProfileOther"
                android:layout_width="42dp"
                android:layout_height="42dp"
                android:adjustViewBounds="true"
                android:contentDescription="@string/content_desc_user_profile"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_contact_picture" >
            </ImageView>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/bubble_white_normal"
        android:gravity="top|center"
        android:orientation="vertical" >

        .... // OTHER STUFF HERE THAT IS NOT NECESSARY IN THIS CODE SNIPPET ON SO

    </LinearLayout>
</LinearLayout>

注1:

虽然,我包括一组工作图像(几乎是勺子喂食,如果你愿意的话),我强烈建议你创建一套适合你的方案的图像。此外,这还将使您能够在将来构建自己的资源。我加倍努力的唯一原因是因为我个人失去了3天,让讲话泡泡看上去正常。 :-(

笔记2:

我将图像设置为LinearLayout的背景。但是,你需要将它设置为你需要看起来像语音泡泡的TextView

其他网站(教程):

  1. http://blog.booleanbites.com/2012/12/android-listview-with-speech-bubble.html
  2. https://github.com/AdilSoomro/Android-Speech-Bubble
  3. http://developer.android.com/reference/android/graphics/NinePatch.html
  4. http://developer.android.com/tools/help/draw9patch.html

1
投票

我认为你将很难尝试使用形状绘图来做到这一点。

我会用9补丁png。

http://developer.android.com/reference/android/graphics/NinePatch.html

基本上,您要么找到/购买图像,要么在您最喜欢的绘图程序中创建一个图像。然后使用draw9patch tool定义可伸展区域,使其能够在View中正确缩放。

这是一个教程,它甚至特定于语音泡泡!

http://adilsoomro.blogspot.co.uk/2012/11/android-how-to-use-9-patch-png.html

它需要一些时间,但它是制作更多设计的可视界面的关键技术。

© www.soinside.com 2019 - 2024. All rights reserved.