Android之自定义Toast提示框样式

来源:互联网 发布:日本饺子 知乎 编辑:程序博客网 时间:2024/06/06 20:06

一:一般提示都是用Toast.makeText提示,为了满足客服要求和大众化用最流行的一种顶部弹出提示框,提示还可以自带图片、

Toast.makeText(this, "提示消息", Toast.LENGTH_SHORT).show();

1、先来效果图、

-----------------转载请注明出处:http://blog.csdn.net/android_cll



二:实现步骤:

1.自定义Toast工具类、

package com.zjtd.bzcommunity.util;import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.zjtd.bzcommunity.R;import java.util.Timer;import java.util.TimerTask;/** * Created by Administrator on 2017/2/17. * Toast自定义工具类 */public class ToastCommom {    private static TextView text;    private static Toast toast;    /**     * 显示Toast     * @param context     * @param tvString     * @param cntime     */    public static void ToastShow(Context context, String tvString, int cntime) {        if (toast == null) {            toast = new Toast(context);            toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);            toast.setDuration(Toast.LENGTH_LONG);            View layout = LayoutInflater.from(context).inflate(R.layout.toast_xml, null);//            ImageView mImageView = (ImageView) layout.findViewById(R.id.iv);//            mImageView.setBackgroundResource(R.drawable.ic_launcher);            text = (TextView) layout.findViewById(R.id.text);            toast.setView(layout);            showMyToast(toast, cntime);        }        text.setText(tvString);        text.setTextColor(0xFFFFFFFF);        text.setTextSize(16);        if(toast!=null){            toast.show();        }    }    //自定义停留时间    public static void showMyToast(final Toast toast, final int cnt) {        final Timer timer = new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                toast.show();            }        }, 0, 3000);        new Timer().schedule(new TimerTask() {            @Override            public void run() {                toast.cancel();                timer.cancel();            }        }, cnt);    }}

2.工具类里面需要的xml布局、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/toast_layout_root"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#90000000"    android:orientation="horizontal"    android:padding="10dp">    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="8dp" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

3.activity或者fragment的引用、


//农场菜金case R.id.caijin_id:    ToastCommom.ToastShow(getActivity(),"农场菜金",3000);    break;

-------------就这么多,自己感觉这种蛮流行和大众化的,大神勿喷哦、


4 0
原创粉丝点击