Toast--自定义Toast的显示风格

在安卓应用中Toast的使用频率是非常高的,关于Toast的基本使用这里就不再啰嗦,这里只说一下怎么去更改Toast的显示风格,让Toast不再是千年不变的黑框框。

先上效果图:

toast.gif

1 方式1 :使用Toast类的setView( )

在Toast类中,有一个 setView( view ) 方法,该方法的作用就是用传入的view替换默认的那个黑框框。

使用setView( ) 方法自定义Toast时大致步骤如下:

  1. 通过Toast 的构造方法创建一个toast对象
  1. 使用LayoutInflater 填充一个view
  2. 将填充起来的view通过setView( ) 设置给toast对象
  3. 调用show( ) 展示吐司。

具体实现代码:
ImageToastActivity.java


/**
 * 作者:CnPeng
 * <p>
 * 时间:2017/2/27:上午10:50
 * <p>
 * 说明:展示带有图片的Toast,自定义吐司布局时,主要依靠setView 方法
 * <p>
 * 关于 负的margin值的注意事项 查看 activity_imagetoast22222222222.xml
 */

public class ImageToastActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagetoast);

        Button button = (Button) findViewById(R.id.bt_imageToast);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_imageToast:

                //自定义Toast 方式1 ,直接setView
                //填充view,官方guide说根布局必须设置id为custom_toast_container,且inflate的第二个参数必须按照这个模式写,实际测试无所谓
                // View toastView = getLayoutInflater().inflate(R.layout.imagetoast, (ViewGroup)findViewById(R.id
                // .custom_toast_container));

                // 填充view
                View toastView = getLayoutInflater().inflate(R.layout.imagetoast, null);

                Toast toast = new Toast(getApplicationContext());           //构造方法创建吐司对象
                toast.setDuration(Toast.LENGTH_SHORT);                      //设置吐司时长
                //toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 300); //更改吐司的展示位置(默认的位置就挺好)
                toast.setView(toastView);                                   //设置吐司的新view
                toast.show();                                               //展示吐司
                break;
        }
    }
}


activity的布局文件中只有一个button,代码省略。


布局文件:imagetoast.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:id="@+id/custom_toast_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!--风格2 吐司左上角展示图标-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="50dp"
        android:background="@drawable/shape_bk_imagetoast"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:text="这是一个带有图片的Toast提示框"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

shape_imagetoast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="#ffdd00"/>
    <stroke android:color="@color/colorAccent" android:width="@dimen/dp1"/>

</shape>

注意:

  1. setView(view)方法中传入的view可以任意定义,但需要注意的是,有些控制view位置的属性可能会不生效。比如负的margin值,具体效果将列举在文章末尾。
  2. 官方guide说根布局view必须设置id为custom_toast_container,且inflate的第二个参数必须按照这个模式写,实际测试无所谓
    View toastView =getLayoutInflater().inflate(R.layout.imagetoast, (ViewGroup)findViewById(R.id.custom_toast_container));

2 方式2 :通过java代码直接修改原生布局

如果要展示的Toast 只是在上方(或者下方、左方、右方)添加一个图片或者更改字号字体背景色等内容,可以直接在java代码中获取原生的布局view并去修改它。

大致步骤如下:

  1. 通过Toast 中的静态方法 makeText( ) 创建一个Toast 对象。
  2. 通过调用toast对象的 getView( ) 方法获取 toast展示的view
  3. 在java代码中修改Toast的显示风格
  4. 调用show( ) 展示Toast

效果图:

toast2.gif

具体实现代码:
ImageToastActivity.java


/**
 * 作者:CnPeng
 * <p>
 * 时间:2017/2/27:上午10:50
 * <p>
 * 说明:展示带有图片的Toast,自定义吐司布局时,主要依靠setView 方法
 * <p>
 * 关于 负的margin值的注意事项 查看 activity_imagetoast22222222222.xml
 */

public class ImageToastActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagetoast);

        Button button = (Button) findViewById(R.id.bt_imageToast);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_imageToast:
                //自定义Toast方式2 : 直接修改原生的toast布局(效果同setView时,view只是一个TextView的情况)
                Toast toast = Toast.makeText(ImageToastActivity.this, "直接改写原生的Toast布局", Toast.LENGTH_SHORT);
                View toastView = toast.getView();
                toastView.setBackgroundResource(R.drawable.shape_bk_imagetoast);
                toastView.setPadding(20, 5, 20, 5);

                TextView tv = (TextView) toastView.findViewById(android.R.id.message);
                tv.setTextSize(16);
                tv.setTextColor(Color.BLUE);
                tv.setGravity(Gravity.CENTER);
                tv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_launcher, 0, 0, 0);
                tv.setCompoundDrawablePadding(20);
                toast.show();

                break;
        }
    }
}

activity的布局文件中只有Button,代码省略。

注意:

  1. toast.getView( ) 可以获取toast展示的布局view
  2. textView 中的drawableLeft 等drawableXXX属性对应的java中的方法是:setCompoundDrawablesWithIntrinsicBounds(leftDrawable , topDrawable, rightDrawable, bottomDrawable); 该方法中传入的四个参数分别对应 drawableLeft , drawableRight, drawableTop , drawableBottom 。如果在某个方向上不需要设置图片就直接传入0。
  3. 通过查看toast的 makeText( ) 方法的源码可以知道,默认的布局文件是:com.android.internal.R.layout.transient_notification,该文件中textView的id是 android:id="@android:id/message"

3 未生效的一个布局文件 :

在这个布局文件中,使用了相对布局,先添加了一个TextView ,然后通过 layout_above 再添加一个ImageView,之后再通过负的margin值控制 imageView 与TextView的相对位置,并通过 layout_alignLeft 让图片的左边框与textView的左边框对齐。然而,这种方式在布局预览中看着是正常的,but,运行之后确实不好使,却展示成下图中的模式,没想明白为啥。如果你知道了请一定要告诉我。。。

Paste_Image.png

详细布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/custom_toast_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--这种在预览的时候没问题,但实际上并不好使!!!-->
    <TextView
        android:id="@+id/tv_imagetoast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/shape_bk_imagetoast"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:text="这是一个带有图片的Toast提示框"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv_imagetoast"
        android:layout_alignLeft="@id/tv_imagetoast"
        android:layout_marginBottom="-15dp"
        android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

4 关于负的margin值 :

关于负的margin 的详细介绍,请参考我之前在CSDN中写的文章,http://blog.csdn.net/north1989/article/details/52922564
(我的CSDN已经暂停更新,以后有新内容会一直在简书更新)

这里只是说明一点:如果负的margin值是相对于Button的,那么button依旧会覆盖该控件!!!!如下图:

Paste_Image.png

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/bt_imageToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="点击展示带有图片的Toast"/>

    <!--相对于Button使用负的margin值时,位置虽然发生了变化,但是button 自带的灰色背景会覆盖控件-->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bt_imageToast"
        android:layout_alignLeft="@id/bt_imageToast"
        android:layout_marginBottom="-20dp"
        android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

以上的代码均为测试过的完整的可用的代码。也可以直接到github下载代码:
https://github.com/CnPeng/CrazyAndroid , 其中的a_36 对应该文章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容