Toast是android的一个简易消息提示框。
它不会获得焦点,也无法被点击。向用户提示信息,却不停留着不动。
其实,自定义Toast非常简单:
先看效果:
首先:新建一个mtoast.xml布局文件:<?xml version="1.0"encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:gravity="center_vertical"
android:orientation="horizontal">
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="34dp">
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="https://img-/202708190215714.png" data-original="@drawable/display_bg_2"/>
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"/>
在定义一个mTools.java文件:publicclassmToolsextendsToast{
privatestaticToastresToast;
publicmTools(Contextcontext){
super(context);
}
publicstaticvoidshowToast(Contextcontext,intresId,CharSequencetext,intduration){
//获取系统的LayoutInflater
LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
Viewview=inflater.inflate(R.layout.mtoast,null);
//实例化控件
ImageViewimageView=(ImageView)view.findViewById(R.id.imageView1);
TextViewtextView=(TextView)view.findViewById(R.id.textView1);
//设置控件要显示的东西
imageView.setImageResource(resId);
textView.setText(text);
//实例化Toast
resToast=newToast(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就出来了!!!!!!!!