Android学习(十八)Toast的使用

一、什么是Toast

1、Toast是一种提供给用户简洁提示信息的视图。

2、该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示。

3、Android提供的Toast类可以创建和显示Toast信息。

下面演示4种Toast的用法,普通的 Toast,更改位置的Toast,显示图片的Toast,自定义的Toast分别使用4个按钮来实现。

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btnShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast显示"/>

    <Button
        android:id="@+id/btnShow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast显示位置"/>

    <Button
        android:id="@+id/btnShow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast显示图片"/>

    <Button
        android:id="@+id/btnShow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义Toast显示layout文件"/>

</LinearLayout>

main.java

package com.example.zhengcheng.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btnshow1;
    Button btnshow2;
    Button btnshow3;
    Button btnshow4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnshow1 = (Button) findViewById(R.id.btnShow);
        btnshow1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "测试Toast弹出消息框", Toast.LENGTH_SHORT);
                toast.show();
            }
        });


        btnshow2 = (Button) findViewById(R.id.btnShow2);
        btnshow2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "更改Toast弹出位置", Toast.LENGTH_SHORT);
                //第一个参数设置Toast的相对位置,第二个参数为x坐标(正为右,负为左),第三个参数为y坐标(正为下,负为上)
                toast.setGravity(Gravity.CENTER, 0, -250);
                toast.show();
            }
        });


        btnshow3 = (Button) findViewById(R.id.btnShow3);
        btnshow3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, " 带图片的Toast弹出对话框", Toast.LENGTH_SHORT);
                //获取toast的布局方式
                LinearLayout toast_layout = (LinearLayout) toast.getView();
                ImageView iv = new ImageView(MainActivity.this);   //创建图片控件
                iv.setImageResource(R.mipmap.ic_launcher);  //设置显示图片
//                toast_layout.addView(iv);   //添加图片控件到toast布局中,默认添加到末尾
                toast_layout.addView(iv, 0);   //添加图片控件到toast布局中,添加到文字之前
                toast.show();  //显示图片
            }
        });

        btnshow4 = (Button) findViewById(R.id.btnShow4);
        btnshow4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取inflater对象,用来加载视图
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View toast_view = inflater.inflate(R.layout.toast_layout,null);
                Toast toast = new Toast(MainActivity.this);  //创建一个Toast
                toast.setView(toast_view);  //设置Toast的视图
                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="30dp"
        android:gravity="center"
        android:text="自定义的Toast对话框" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="显示Toast弹出的内容" />
</LinearLayout>
原文地址:https://www.cnblogs.com/zhengcheng/p/4423468.html