实现界面:
下面的代码里有注释帮助理解
常用方法:
Toast.makeText(ToastActivity.this,"要显示的文字", Toast.LENGTH_SHORT).show();
//显示Toast,参数:当前页面内容,要显示的文字,显示的时长(Long与Short)
Toast.setGravity(Gravity.CENTER,0,0) //设置居中
Toast.setView(view); //自定义Toast的view
实现代码:
ToastActivity:java文件
package com.example.administrator.exercise;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.zip.Inflater;
public class ToastActivity extends AppCompatActivity implements View.OnClickListener{
Button button_toast_default,button_toast_center,button_toast_design;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast);
button_toast_default = (Button) findViewById(R.id.toast_default);
button_toast_center = (Button) findViewById(R.id.toast_center);
button_toast_design = (Button) findViewById(R.id.toast_design);
button_toast_default.setOnClickListener(this);
button_toast_center.setOnClickListener(this);
button_toast_design.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.toast_default:{
Toast.makeText(ToastActivity.this,"默认Toast", Toast.LENGTH_SHORT).show(); //显示Toast
break;
}
case R.id.toast_center:{
Toast ToastCenter = Toast.makeText(ToastActivity.this,"居中Toast", Toast.LENGTH_SHORT); //创建Toast
ToastCenter.setGravity(Gravity.CENTER,0,0); //设置居中
ToastCenter.show(); //显示Tosat
break;
}
case R.id.toast_design:{
Toast ToastDesign = new Toast(getApplicationContext()); //创建Toast
LayoutInflater layoutInflater = LayoutInflater.from(ToastActivity.this); //创建布局填充器
View view1 = layoutInflater.inflate(R.layout.toast_item,null); //填充布局
ImageView imageView = (ImageView) view1.findViewById(R.id.toast_image); //连接ImageView
TextView textView = (TextView) view1.findViewById(R.id.toast_title); //连接TextView
imageView.setImageResource(R.drawable.icon_qq); //设置ImageView的图片
textView.setText("自定义Toast"); //设置TextView的文字
ToastDesign.setView(view1); //添加view到Toast
ToastDesign.show(); //显示Toast
break;
}
}
}
}
activity_toast.xml:布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_toast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.exercise.ToastActivity">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="默认Toast"
android:textSize="16sp"
android:id="@+id/toast_default"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="居中Toast"
android:textSize="16sp"
android:id="@+id/toast_center"
android:layout_below="@+id/toast_default"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="自定义Toast"
android:textSize="16sp"
android:id="@+id/toast_design"
android:layout_below="@+id/toast_center"/>
</RelativeLayout>
toast_item:自定义Toast的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/img01"
android:scaleType="centerCrop"
android:id="@+id/toast_image"/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="夜景"
android:gravity="center"
android:textSize="16sp"
android:textColor="@color/colorBlack"
android:id="@+id/toast_title"/>
</LinearLayout>
注:自定义Toast需要自定义布局文件,来提供自定义的视图view