自定义的五种Toast

自定义的五种Toast,希望能够帮助到大家,上代码!

首先是activity_main.xml里面的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/toast_btn_1" >
    </Button>

    <Button
        android:id="@+id/button_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/toast_btn_2" >
    </Button>

    <Button
        android:id="@+id/button_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/toast_btn_3" >
    </Button>

    <Button
        android:id="@+id/button_4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/toast_btn_4" >
    </Button>

    <Button
        android:id="@+id/button_5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/toast_btn_5" >
    </Button>

</LinearLayout>

编写好布局之后,我们就来看看自定义Toast到底是怎么写的

package com.mytoast;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
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 {
	//初始化显示的信息
	//初始化Button
	private Button toastBtn_1, toastBtn_2, toastBtn_3, toastBtn_4, toastBtn_5;
	private Toast toast = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		toastBtn_1 = (Button) findViewById(R.id.button_1);
		toastBtn_2 = (Button) findViewById(R.id.button_2);
		toastBtn_3 = (Button) findViewById(R.id.button_3);
		toastBtn_4 = (Button) findViewById(R.id.button_4);
		toastBtn_5 = (Button) findViewById(R.id.button_5);
		toastBtn_1.setOnClickListener(this);
		toastBtn_2.setOnClickListener(this);
		toastBtn_3.setOnClickListener(this);
		toastBtn_4.setOnClickListener(this);
		toastBtn_5.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// AlertDialog.Builder builder;
		// AlertDialog dialog;
		switch (v.getId()) {
		case R.id.button_1:
			toast.makeText(this, "这是默认的Toast显示", Toast.LENGTH_LONG).show();
			break;

		case R.id.button_2:
			toast = Toast.makeText(getApplicationContext(), "这是自定义位置的Toast显示",
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			break;

		case R.id.button_3:
			toast = Toast.makeText(getApplicationContext(), "这是带图片的Toast显示",
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 50, -100);
			LinearLayout layout = (LinearLayout) toast.getView();
			ImageView image = new ImageView(getApplicationContext());
			image.setImageResource(R.drawable.wallpaper_tree_small);
			layout.addView(image, 0);
			toast.show();
			break;

		case R.id.button_4:
			View view = LayoutInflater.from(getApplicationContext()).inflate(
					R.layout.userdefinedtoast, null);
			toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setView(view);
			// toast.show();
			// 设置延迟五秒显示
			myToast();
			break;

		case R.id.button_5:
			View view1 = LayoutInflater.from(getApplicationContext()).inflate(
					R.layout.dialog_custom, null);
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setView(view1);
			AlertDialog alertDialog = builder.create();
			alertDialog.show();
			break;
		}
	}

	// 设置延迟生效
	private void myToast() {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				initToast();
			}
		}, 5000);
	}

	private void initToast() {
		toast.show();
	}

}

这样一来就能够显示了,dialog_custom.xml的话我是这样写的,当然这都是随意的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout"
    android:layout_width="200dip"
    android:layout_height="fill_parent"
    android:background="#111111"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:text="@string/toast_text_1"
        android:textColor="#ffffff"
        android:textSize="20sp" >
    </TextView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#999999"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/image_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip"
            android:src="@drawable/wallpaper_field_small" >
        </ImageView>

        <TextView
            android:id="@+id/txt_context"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:text="@string/toast_text_2"
            android:textColor="#ffffff"
            android:textSize="15dip" >
        </TextView>
    </LinearLayout>

</LinearLayout>

这样一来,完美的自定义Toast就能够实现了,都快试试吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值