在我的应用程序中,我想为 TextView 设置气泡,在 TextView 中我添加了 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 中制作它。
最佳答案
您需要使用的基本上是一个 9-patch 图像(正如 Ken Wolf 在此评论中已经指出的那样)。
为了让您开始,我将包含一组来 self 的一个应用程序的 9 个补丁图像,以及一段简短的代码,说明如何在创建布局 XMl 时使用它。 ;-)
9 补丁图像集:
(分别命名为:bubble_white_normal_mdpi.9
、bubble_white_normal_hdpi.9
和 bubble_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
,您需要看起来像一个语音气泡。
其他网站(教程):
关于android - 如何将气泡添加到 textview android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541739/