android - 如何将气泡添加到 textview android?

标签 android android-intent android-custom-view nine-patch

在我的应用程序中,我想为 TextView 设置气泡,在 TextView 中我添加了 setBackgroundResource(),正如您在代码中看到的那样。

通过这段代码,我得到了这样的图像:

enter image description here

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

enter image description here

<?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 中制作它。

最佳答案

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

为了让您开始,我将包含一组来 self 的一个应用程序的 9 个补丁图像,以及一段简短的代码,说明如何在创建布局 XMl 时使用它。 ;-)

9 补丁图像集:

mdpi hdpi xhdpi

(分别命名为:bubble_white_normal_mdpi.9bubble_white_normal_hdpi.9bubble_white_normal_xhdpi.9删除将文件名中的 _mdpi_hdpi_xhdpi 放在各自的 drawable 文件夹中。

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

关于android - 如何将气泡添加到 textview android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541739/

相关文章:

java - 从 android 在 openfire 服务器中创建新用户

android - 如何为矢量绘图添加阴影?

android - Activity 生命周期困惑

android - 无法将自定义 View 添加到 Android 操作栏,为什么?

android - setClipBounds 可以在 View 绘制之前调用吗?

java - 使用 Gson 创建 JSON 会使应用程序崩溃

android - 在启动器中没有图标的市场中提供 "open"

android - 虽然我正在使用 classname.this 仍然收到 "The constructor Intent(MySampleFragment, Class<Alert>) is undefined"错误

android - 如何在通货膨胀期间从另一个控件中引用一个控件?

android - 如何在 Android 上的相机预览中粘贴内容?