android– 在吐司中显示图像

android– 在吐司中显示图像,第1张

概述我在我的android项目中使用toast来显示文本.我可以使用toasts来显示图像d出消息.如果是这样,有人可以给我一些有用的代码段.谢谢!解决方法:在/res/layout文件夹中创建文件’toast.xml’,并将以下内容复制到文件中并保存文件.        <ImageViewandroid:id="@+id/ima

我在我的android项目中使用toast来显示文本.我可以使用toasts来显示图像d出消息.如果是这样,有人可以给我一些有用的代码段.谢谢!

解决方法:

在/ res / layout文件夹中创建文件’toast.xml’,并将以下内容复制到文件中并保存文件.
    
    

<ImageVIEw androID:ID="@+ID/image"androID:layout_wIDth="wrap_content"androID:layout_height="fill_parent"androID:layout_marginRight="1dp" /><TextVIEw androID:ID="@+ID/text"androID:layout_wIDth="wrap_content"androID:layout_height="fill_parent"androID:layout_marginRight="4dp"androID:textcolor="#FFF" />    </linearLayout>

在相同的/ res / layout文件夹中,您必须创建一个roundborder.xml文件,其中背景的布局位于该文件中.在这种情况下,圆形边框具有一些背景透明度.

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID"><corners androID:radius="4dp" /><padding androID:left="2dp"androID:top="2dp"androID:right="2dp"androID:bottom="2dp" /><solID androID:color="#CC4D404D" /><stroke androID:wIDth="2dp" androID:color="#CC8D808D" /></shape>

吐司布局将通过R类提供.现在,我们必须使用toast布局创建一个VIEw.为此,我们必须膨胀xml文件. xl文件的名称用作第一个参数R.layout.toast.作为第二个参数,使用linearLayout的ID.

VIEw toastVIEw = getLayoutInflater().inflate(R.layout.toast,
(VIEwGroup)findVIEwByID(R.ID.toastLayout));

接下来,我们必须将图像分配给ImageVIEw,并将消息(显示)分配给TextVIEw.您可以使用资源标识符或可绘制的位图作为图像.

ImageVIEw imageVIEw = (ImageVIEw)toastVIEw.findVIEwByID(R.ID.image);imageVIEw.setimageResource(R.drawable.icon);//    imageVIEw.setBackgroundDrawable(bitmapDrawable);TextVIEw textVIEw = (TextVIEw)toastVIEw.findVIEwByID(R.ID.text);textVIEw.setText("Yes, a Toast with an image!");

最后,我们必须创建和展示Toast,我们已经完成了.

Toast toast = new Toast(context);toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);toast.setDuration(Toast.LENGTH_LONG);toast.setVIEw(toastVIEw);toast.show();
总结

以上是内存溢出为你收集整理的android – 在吐司中显示图像全部内容,希望文章能够帮你解决android – 在吐司中显示图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1110388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存