Toast.makeText是Android中用于显示短暂提示信息的类。它有几种常见的用法:
Toast.makeText(context, "提示信息", Toast.LENGTH_SHORT).show();
Toast toast = Toast.makeText(context, "提示信息", Toast.LENGTH_SHORT);
toast.setDuration(5000); // 设置为自定义时长,单位为毫秒
toast.show();
Toast toast = Toast.makeText(context, "提示信息", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置为顶部居中显示
toast.show();
LayoutInflater inflater = getLayoutInflater();
View toastView = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.custom_toast_container));
Toast toast = new Toast(context);
toast.setView(toastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
这些是Toast.makeText的几种常见用法,可以根据具体需求选择适合的方式来显示Toast提示信息。