<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.leeson7_1_id19.MainActivity">
 
     <Button
         android:onClick="toast_1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="普通的toast" />
     <Button
         android:onClick="toast_2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="自定义的toast" />
     <Button
         android:onClick="toast_3"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="自定义位置的toast" />
 </LinearLayout>
<?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="wrap_content"
         android:text="自定义位置文本"
         android:gravity="center"
         android:background="#ff0000"
         android:padding="5dp"
         android:textSize="18sp"/>
 
 
 </LinearLayout>
package com.example.leeson7_1_id19;
 
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.Toast;
 
 public class MainActivity extends AppCompatActivity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
     public void toast_1(View V){
         // 设置土司显示的内容和时长并显示出来
         Toast.makeText(this,"我是一个普通的toast",Toast.LENGTH_SHORT).show();
     }
     public void toast_2(View V){
         // 自定义土司
         // 创建土司
         Toast toast = new Toast(this);
         // 设置土司显示的时间长短
         toast.setDuration(Toast.LENGTH_SHORT);
         // 创建ImageView
         ImageView img = new ImageView(this);
         // 设置图片的资源路径
         img.setImageResource(R.mipmap.ic_launcher);
         // 设置土司的视图图片
         toast.setView(img);
         // 显示土司
         toast.show();
     }
     public void toast_3(View V){
         // 自定义土司显示位置
         // 创建土司
         Toast toast = new Toast(this);
         // 找到toast布局的位置
         View layout = View.inflate(this,R.layout.toast,null);
         // 设置toast文本,把设置好的布局传进来
         toast.setView(layout);
         // 设置土司显示在屏幕的位置
         toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.TOP,0,70);
         // 显示土司
         toast.show();
     }
 }