Toast的几种使用方法

结合自己的使用经验,将toast的几种使用方法汇总如下

首先上几张效果图片:



java文件如下:

package com.my;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyToast extends Activity  implements OnClickListener{
    
	private Button button1,button2,button3,button4;
	 @Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mytoast);
		button1=(Button)findViewById(R.id.button1);
		button2=(Button)findViewById(R.id.button2);
		button3=(Button)findViewById(R.id.button3);
		button4=(Button)findViewById(R.id.button4);
	
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
	
		
		
		
	}
	 
	 public void onClick(View view)
	 {
		if(view==button1)//系统默认的风格
		{
			Toast.makeText(getApplicationContext(), "系统默认的提示框",
					Toast.LENGTH_SHORT).show();
		}
		if(view==button2)//自定义显示位置
		{
			Toast toast=Toast.makeText(getApplicationContext(), "自定义位置", Toast.LENGTH_SHORT);
		    toast.setGravity(Gravity.CENTER, 0, 0);
		    toast.show();
		    
		
		}
		if(view==button3)//带图片
		{
			Toast toast=Toast.makeText(getApplicationContext(), "带图片", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			LinearLayout  toastView=(LinearLayout) toast.getView();
			ImageView  image=new ImageView(this);
			image.setImageResource(R.drawable.sample_thumb_0);
			toastView.addView(image, 0);
			toast.show();
			
		}
		if(view==button4)//完全自定义
		{
			LayoutInflater  inflate=getLayoutInflater();
			View  v=inflate.inflate(R.layout.mytoastdemo, (ViewGroup)findViewById(R.id.llToast));
			//备注:下面的V.findViewById(R.id.textView1)前面的V不能丢,不然很容易导致出错的
			TextView tv=(TextView)v.findViewById(R.id.textView1);
		    tv.setText("注意");
		    ImageView image=(ImageView)v.findViewById(R.id.imageView1);
		   
		    image.setImageResource(R.drawable.sample_thumb_1);
		    Toast toast = new Toast(getApplicationContext());
		    toast.setGravity(Gravity.CENTER, 0, 0);
		    toast.setDuration(Toast.LENGTH_LONG);
		    toast.setView(v);
		    toast.show();
		
		}
		
		
	 }
}

mytoast.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" >



    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="系统默认的提示框" />



    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义显示位置" />



    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="带图片效果" />



    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="完全自定义的Toast" />




</LinearLayout>

mytoastdemo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/llToast"
    android:background="#ffffffff" 
    android:orientation="vertical" >




    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_margin="1dip"/>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>



    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="完全自定义的Toast" />

</LinearLayout>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值