100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android中自定义 toast android 自定义Toast

android中自定义 toast android 自定义Toast

时间:2019-04-20 21:30:16

相关推荐

android中自定义 toast android 自定义Toast

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就出来了!!!!!!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。