几种不同风格的Toast

一般情况下,我们使用Toast默认的风格就行了,只是有些时候为了达到我们自己想要的效果需要自定义一下,包括自定义显示的位置,显示的内容以及完全自定义里面的布局,代码如下:

activity:

package com.home.testtoast;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
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 MainActivity extends Activity implements OnClickListener {
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private static final String TOASTBTN_1 = "这是默认的Toast显示";
	private static final String TOASTBTN_2 = "这是自定义位置的Toast显示";
	private static final String TOASTBTN_3 = "这是带图片的Toast显示";
	private static final String TOASTBTN_4 = "这是完全自定义的Toast显示";
	private Toast toast = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn1 = (Button) findViewById(R.id.main_btn1);
		btn2 = (Button) findViewById(R.id.main_btn2);
		btn3 = (Button) findViewById(R.id.main_btn3);
		btn4 = (Button) findViewById(R.id.main_btn4);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == btn1) {
			toast.makeText(this, TOASTBTN_1, Toast.LENGTH_LONG).show();
		}
		if (v == btn2) {
			toast = Toast.makeText(getApplicationContext(), TOASTBTN_2,
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
		if (v == btn3) {
			toast = Toast.makeText(getApplicationContext(), TOASTBTN_3,
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 50, -100);
			LinearLayout layout = (LinearLayout) toast.getView();
			ImageView image = new ImageView(getApplicationContext());
			image.setImageResource(R.drawable.image1);
			layout.addView(image, 0);
			toast.show();
		}
		if (v == btn4) {
			View view = getLayoutInflater().inflate(R.layout.userdefinedtoast,
					(ViewGroup) findViewById(R.id.toast_layout));
			TextView txtView_Title = (TextView) view
					.findViewById(R.id.txt_Title);
			TextView txtView_Context = (TextView) view
					.findViewById(R.id.txt_context);
			ImageView imageView = (ImageView) view
					.findViewById(R.id.image_toast);
			toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(view);
			toast.show();
		}
	}

}

main.xml:

<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/main_btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="默认的Toast" />

    <Button
        android:id="@+id/main_btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="自定义位置的Toast" />

    <Button
        android:id="@+id/main_btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="带图片的Toast" />

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

</LinearLayout>


自定义的Toast布局:userdefinedtoast.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="自定义Toast"
        android:textColor="#ffffff"
        android:textSize="20dip" >
    </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/image2" >
        </ImageView>

        <TextView
            android:id="@+id/txt_context"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:text="这是自定义Toast,左边的是一幅很美的图片"
            android:textColor="#ffffff"
            android:textSize="15sp" >
        </TextView>
    </LinearLayout>

</LinearLayout>

附上图片效果:



 

posted on 2013-12-02 15:05  我的小人生  阅读(318)  评论(0编辑  收藏  举报