如何自定义吐司在Android中 [英] How to Customize Toast In Android

查看:165
本文介绍了如何自定义吐司在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和工作的一个示例应用程序。我想了解我们如何可以自定义默认的Andr​​oid吐司。 我想改变面包的颜色,样式和其他属性。

I am new to Android and working on a sample app. I want to learn how can we customize the default Android Toast. I want to change the color, Style and other attribute of Toast.

我们能在面包中添加图片呢?

我看了下面的帖子在计算器

I read following post on stackOverflow

如何自定义吐司在Android的?在安卓定制面包

但这些解释如何添加图像吐司。

but none of these explains how to add image in Toast.

推荐答案

是的,我们可以改变颜色,大小,位置,和烤面包的其他属性。 我们还可以添加图像敬酒。

Yes we can change the color, size , position, and other attributes of Toast. We can also add an Image to Toast.

一个好的博客这个 如何定制吐司在Android中所有的内容从这个博客采取

A good blog for this How To Customize Toast In Android All the contents are taken from this blog

您可以创建一个XML和它充气吐司。

You can create an XML and inflate it Toast.

您也可以做到这一点在运行时

You can also do it at run time

    LinearLayout  layout=new LinearLayout(this);
    layout.setBackgroundResource(R.color.LightOrange);

    TextView  tv=new TextView(this);
    // set the TextView properties like color, size etc
    tv.setTextColor(Color.RED);
    tv.setTextSize(15);        

    tv.setGravity(Gravity.CENTER_VERTICAL);

    // set the text you want to show in  Toast
    tv.setText("My Custom Toast at Bottom of Screen");  

   ImageView   img=new ImageView(this);

    // give the drawble resource for the ImageView
    img.setImageResource(R.drawable.myimage);

  // add both the Views TextView and ImageView in layout
    layout.addView(img);
    layout.addView(tv);

    Toast toast=new Toast(this); //context is object of Context write "this" if you are an Activity
   // Set The layout as Toast View
    toast.setView(layout);

      // Position you toast here toast position is 50 dp from bottom you can give any integral value   
     toast.setGravity(Gravity.BOTTOM, 0, 50);
     toast.show();

这篇关于如何自定义吐司在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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