Android Toast 默认和自定义使用

来源:互联网 发布:未来软件的发展趋势 编辑:程序博客网 时间:2024/05/18 13:47
Toast是一种简易的消息提示框,和Dialog不一样的是,Toast永远不会获得焦点,无法被点击。
Toast的设计思想就是尽可能不太引人注意,同时还能向用户展示信息,希望他们看到。

Toast显示的时间有限,只分为long和short,它会根据用户设置的显示时间后自动消失。


1.默认Toast

<span style="font-size:18px;">myToast1("这是默认的Toast");</span>
<span style="font-size:18px;">public void myToast1(String str) {        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();}</span>
参数依次是显示Toast的上下文、显示的字符串、显示时间、最后show出来即可。

2.自定义Tosat

先大致了解一下LayoutInflater

作用: 
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素. 

方法: 
 1、Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面. 
 2、其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml, 

区别:
1、 LayoutInflater.inflate是加载一个布局文件。

2、 findViewById则是从布局文件中查找一个控件;

<span style="font-size:18px;">myToast2("这是郭峰自定义的Toast");</span>
<span style="font-size:18px;">public void myToast2(String str) {        LayoutInflater inflater = getLayoutInflater();        //作用就是将一个xml定义的布局文件实例化为view控件对象;        //1.reSource:View的layout的ID        //2.root:需要附加到resource资源文件的根控件,inflate()会返回一个View对象,        // 如果第三个参数attachToRoot为true,就将这个root作为根对象返回,        // 否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上。        // 如果root为null则会忽略view根对象的LayoutParams属性(注意)。        //3.attachToRoot:是否将root附加到布局文件的根视图上        View view = inflater.inflate(                R.layout.demo11_toast,                (ViewGroup) findViewById(R.id.R_toast),                false        );        //设置ImageView        ImageView img_toast = (ImageView) view.findViewById(R.id.img_toast);        img_toast.setImageResource(R.mipmap.ic_launcher);        //设置TextView        TextView tv_msg = (TextView) view.findViewById(R.id.tv_toast);        tv_msg.setText(str);        //上下文        Toast toast = new Toast(getApplicationContext());        //设置显示位置,        toast.setGravity(Gravity.CENTER, 0, 0);        //设置时间        toast.setDuration(Toast.LENGTH_LONG);        //设置View        toast.setView(view);        //显示Tosat        toast.show(); }</span>
demo11_toast.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/R_toast"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/demo11_bg_toast">    <ImageView        android:id="@+id/img_toast"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_toast"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toRightOf="@+id/img_toast"        android:textSize="14sp" /></RelativeLayout>

demo11_bg_toast.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <!--设置透明背景色-->    <solid android:color="#3989cd" />    <!--设置黑色边框-->    <stroke        android:width="1px"        android:color="#FFFFFF" />    <!--设置四个圆角半径-->    <corners        android:bottomLeftRadius="50px"        android:bottomRightRadius="50px"        android:topLeftRadius="50px"        android:topRightRadius="50px" />    <!--设置边距,让空间大一点-->    <padding        android:bottom="4dp"        android:left="7dp"        android:right="7dp"        android:top="4dp" /></shape>
好了,基本的Toast就这么多,有错误请指正。



0 0