您的位置:首页 > 移动开发 > Android开发

自定义toast

2015-11-03 12:05 381 查看
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,使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 自定义toast