UI组件之弹出组件(Toast)
Toast是一个消息提示组件
设置显示的位置
自定义显示内容(示例:添加一个图片)
简单封装
Toast默认是屏幕底部位置弹出:
Toast.makeText(ToastActivity.this,"Toast",Toast.LENGTH_LONG).show();
Toast弹出位置居中显示:
Toast toastCenter = Toast.makeText(getApplicationContext(),"居中Toast",Toast.LENGTH_LONG);
toastCenter.setGravity(Gravity.CENTER,0,0);
toastCenter.show();
自定义显示内容:
case R.id.btn_toast_3:
Toast toastCustom = new Toast(getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
View view = inflater.inflate(R.layout.layout_toast, null);
ImageView imageView = view.findViewById(R.id.iv_toast);
TextView textView = view.findViewById(R.id.tv_toast);
imageView.setImageResource(R.drawable.smile);
textView.setText("自定义Toast");
toastCustom.setView(view);
toastCustom.setDuration(Toast.LENGTH_LONG);
toastCustom.show();
break;
显示效果:图片加文字居中显示
关于Toast的简单封装:
public class ToastUtil {
public static Toast mToast;
public static void showMsg(Context context,String msg){
if(mToast == null){
mToast = Toast.makeText(context,msg,Toast.LENGTH_LONG);
}else {
mToast.setText(msg);
}
mToast.show();
}
}
//使用 activity中
case R.id.btn_toast_4:
ToastUtil.showMsg(getApplicationContext(),"封装Toast");
break;
网友评论