自定义的五种Toast

来源:互联网 发布:nginx并发数设置 编辑:程序博客网 时间:2024/05/17 15:37

自定义的五种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 {//初始化显示的信息//初始化Buttonprivate Button toastBtn_1, toastBtn_2, toastBtn_3, toastBtn_4, toastBtn_5;private Toast toast = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);}@Overridepublic 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() {@Overridepublic 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就能够实现了,都快试试吧。

0 0