如何将气泡添加到 textview android? [英] How to add the bubbles to textview android?

查看:39
本文介绍了如何将气泡添加到 textview android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想将气泡设置为文本视图,在文本视图中我添加了 setBackgroundResource(),如您在代码中所见.

In my application, I want to set bubbles to a text view, in the text view I add the setBackgroundResource() as you can see in the code.

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

With this code i'm getting an image like this:

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

I want a bubble shape image like this:

<?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 中实现这一点.

Please tell me how to make this in my setBackgroundResource() XML.

推荐答案

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

What you need to use is essentially a 9-patch image (As already pointed out by Ken Wolf in this comment).

为了帮助您入门,我提供了一组来自我的一个应用程序的 9 个补丁图像以及一段关于如何在创建布局 XMl 时使用它的简短代码.;-)

To get you started, I am including a set of 9-patch images from one of my apps along with a brief piece of code on how to use it when creating a layout XMl. ;-)

9-patch 图像集:

(它们分别命名为:bubble_white_normal_mdpi.9bubble_white_normal_hdpi.9bubble_white_normal_xhdpi.9.删除_mdpi_hdpi_xhdpi 将它们放入各自的 drawable 文件夹后,来自文件名.

(These are named: bubble_white_normal_mdpi.9, bubble_white_normal_hdpi.9 and bubble_white_normal_xhdpi.9 respectively. Remove the _mdpi, _hdpi and _xhdpi from the file names after placing them in their respective drawable folders.

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 天时间让对话气泡看起来和正常工作.:-(

Although, I am including a working set of Images (almost spoon feeding, if you will), I would strongly urge you to create your own set of images that fit in your scheme of things. Plus, this will also equip you to build your own resources in the future. The only reason I am going the extra mile is because I personally lost 3 days getting the speech bubble looking and working right. :-(

注意 2:

我将图像设置为 LinearLayout 的背景.但是,您需要将其设置为 TextView,您需要使其看起来像一个语音气泡.

I am setting the image as a background to a LinearLayout. You, however, will need to set it to the TextView you need looking like a Speech Bubble.

其他网站(教程):

  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

这篇关于如何将气泡添加到 textview android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆