自定义toast

来源:互联网 发布:免费手机阅读软件 编辑:程序博客网 时间:2024/06/01 10:24
public class GCSimpleToast {  private static LayoutInflater mInflater;  private static Toast mToast;  private static View mView;  public static void ok(Context context, String msg) {    mInflater = LayoutInflater.from(context);    mView = mInflater.inflate(R.layout.toast_ok, null);      initSetTextViewMsg(msg);    mToast = new Toast(context);    mToast.setView(mView);    mToast.setDuration(Toast.LENGTH_SHORT);    mToast.show();  }

1,准备好你将需要的布局

private static TextView initSetTextViewMsg(String msg) {      TextView mTextView = (TextView) mView.findViewById(R.id.tv_toast);      mTextView.setText(msg);    return mTextView;  }

2,toast信息

    public enum BGToast {        OK,//蓝色        ERROR,//红色        INFO,//绿色        MUTED,//灰色        WARNING//橙色    }    public static void configBG(BGToast bgToast, String message) {        switch (bgToast) {            case OK:                GCSimpleToast.ok(GCApp.getInstance(), message);                break;            case ERROR:                GCSimpleToast.error(GCApp.getInstance(), message);                break;            case INFO:                GCSimpleToast.info(GCApp.getInstance(), message);                break;            case MUTED:                GCSimpleToast.muted(GCApp.getInstance(), message);                break;            case WARNING:                GCSimpleToast.warning(GCApp.getInstance(), message);                break;        }    }

3,枚举,选择需要的样式

public void showMessage(final String message) {        runOnUiThread(new Runnable() {            @Override            public void run() {                GCSimpleToast.info(GCBaseFragmentActivity.this, message);            }        });    }

 public void showMessage(final String message, final GCSimpleToast.BGToast type) {        runOnUiThread(new Runnable() {            @Override            public void run() {                GCSimpleToast.configBG(type, message);            }        });    }

4,在基类里封装一下

showMessage(getResources().getString(R.string.toast_new_version), GCSimpleToast.BGToast.INFO);

showMessage(getResources().getString(R.string.toast_new_version));

5,使用。

1 0
原创粉丝点击