2015/9/14

2015/9/14

膜拜亮大神之—-Toast

不单纯的Toast,先上代码。

package com.zsz.develop.mytoast.mytoast;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zsz.develop.mytoast.R;


/**
 * @author LigthWang
 * 
 *         自定义土司
 */
public class MyToast {

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 static final String TOASTBTN_5 = "这是长时间的Toast显示";
private static Toast toast = null;

public static void showToast(int witch, Activity context) {
    AlertDialog.Builder builder;
    AlertDialog dialog;
    switch (witch) {
    case 0:
        toast.makeText(context, TOASTBTN_1, Toast.LENGTH_LONG).show();
        break;

    case 1:
        toast = Toast.makeText(context, TOASTBTN_2, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        break;
    case 2:
        toast = Toast.makeText(context, TOASTBTN_3, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 50, -100);
        LinearLayout layout = (LinearLayout) toast.getView();
        ImageView image = new ImageView(context);
        image.setImageResource(R.drawable.wallpaper_tree_small);
        layout.addView(image, 0);
        toast.show();
        break;
    case 3:
        LayoutInflater inflater = context.getLayoutInflater();
        View view = inflater.inflate(R.layout.userdefinedtoast,
                (ViewGroup) context.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(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
        break;
    case 4:
        LayoutInflater inflater1 = context.getLayoutInflater();
        View view1 = inflater1.inflate(R.layout.userdefinedtoast,
                (ViewGroup) context.findViewById(R.id.toast_layout));
        TextView txtView_Title1 = (TextView) view1
                .findViewById(R.id.txt_Title);
        TextView txtView_Context1 = (TextView) view1
                .findViewById(R.id.txt_context);
        ImageView imageView1 = (ImageView) view1
                .findViewById(R.id.image_toast);
        builder = new AlertDialog.Builder(context);
        builder.setView(view1);
        dialog = builder.create();
        dialog.show();
        break;
    }
}

}

需要通过加载xml的时候的Toast,这样就让Toast实现了彻底自定义。

<?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="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/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>

github代码地址:https://github.com/zszdevelop/WorshipLight