Toast

来源:互联网 发布:2016淘宝不能邮箱注册 编辑:程序博客网 时间:2024/05/16 11:59

Toast是一种提供给用户简洁提示信息的视图,该视图以浮于应用程序之上的形式呈现给用户

Toast提示界面不获取焦点,所以不影响用户的操作

Toast提示就是在不影响用户使用程序的同时,给用户提供某些提示信息

有两个例子就是音量控制和设置信息保存成功


常用方法

Toast.makeText(context, text, duration); // 返回值为Toasttoast.setDuration(duration); // 设置持续时间toast.setGravity(gravity, xOffset, yOffset); // 设置toast位置toast.setText(s); // 设置提示内容toast.show(); // 显示

显示默认Toast

//    Toast toast = Toast.makeText(this, "这是一个默认的Toast", 1000);    Toast toast = Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT);    toast.show();


显示自定义位置的Toast
    Toast toast = Toast.makeText(this, "改变位置的Toast", Toast.LENGTH_SHORT);    toast.setGravity(Gravity.CENTER, -50, -100);// 向右向下为正    toast.show();


带有图片的Toast
    Toast toast = Toast.makeText(this, "带有图片的Toast", Toast.LENGTH_SHORT);    LinearLayout toast_layout = (LinearLayout) toast.getView();    ImageView iv = new ImageView(this);    iv.setImageResource(R.drawable.ic_launcher);//    toast_layout.addView(iv);    toast_layout.addView(iv, 0); // 图片在文字上 index指定为0    toast.show();


完全自定义的Toast


新建一个布局toast_layout.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView        android:layout_width="match_parent"        android:layout_height="30dip"        android:gravity="center"        android:text="这个是自定义的Toast"        />        <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher"        />        <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:text="内容部分,我们可以随便写"        />    </LinearLayout>


    LayoutInflater inflater = LayoutInflater.from(this);    View toast_view = inflater.inflate(R.layout.toast_layout, null);    Toast toast = new Toast(this);    toast.setView(toast_view);    toast.show();


0 0
原创粉丝点击