为了账号安全,请及时绑定邮箱和手机立即绑定

android 自定义Toast

标签:
Android

Toast是android的一个简易消息提示框。

         它不会获得焦点,也无法被点击。向用户提示信息,却不停留着不动。

        其实,自定义Toast非常简单:

         先看效果:

                5be45b1100015b1703020135.jpg

          首先:新建一个mtoast.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="34dp" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/display_bg_2" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:textColor="@android:color/white"
            android:text="TextView" />
    </RelativeLayout>
</LinearLayout>

在定义一个mTools.java文件:

    public class mTools extends Toast{
    private static Toast resToast;
public mTools(Context context) {
super(context);
}
public static void  showToast(Context context,int resId,CharSequence text,int duration){
//获取系统的LayoutInflater
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
View view=inflater.inflate(R.layout.mtoast, null);
//实例化控件
ImageView imageView=(ImageView) view.findViewById(R.id.imageView1);
TextView textView=(TextView) view.findViewById(R.id.textView1);
//设置控件要显示的东西
imageView.setImageResource(resId);
textView.setText(text);
//实例化Toast
resToast=new Toast(context);
resToast.setView(view);     //设置要显示的view
resToast.setGravity(Gravity.CENTER, 0,0); //设置在屏幕的什么位置显示
resToast.setDuration(duration); //显示的时间长短
resToast.show();  //show
}
}

那么你只需要在Activity中

           mTools.showToast(MainActivity.this,R.drawable.display_bg_2,"测试Toast", Toast.LENGTH_SHORT);

就OK了,一个自定义的Toast就出来了!!!!!!!!

原文链接:http://www.apkbus.com/blog-524160-59269.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消