您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Android中怎么使用Toast自定义图片和文字?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
自定义工具类代码
public class ToastUtils {
private static Context mContext = OcreanSonicApplication.getContext();
public static void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
/**
* 带图片的吐司提示
* @param text
*/
public static void showCustomImgToast(String text) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 带图片的吐司提示
* 通过参数传递,可是设置吐司的图片和文字内容
* @param text
*/
public static void showCustomImgToast(String text,int imgResId) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 不带图片的吐司提示
* @param text
*/
public static void showCustomToast(String text) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setVisibility(View.GONE);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
/**
* 带图片的吐司,设置吐司弹出的位置为屏幕中心
* @param text
*/
public static void showCustomToastCenter(String text) {
showCustomToastCenter(text, R.mipmap.pd_ic_finish);
}
/**
* 带图片的吐司,设置吐司弹出的位置为屏幕中心
* 通过参数传递,可是设置吐司的图片和文字内容
* @param text
*/
public static void showCustomToastCenter(String text, int imgResId) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(imgResId);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
Toast toast = null;
if (toast != null) {
toast.cancel();
}
toast = new Toast(mContext);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}在自定义Toast中引用xml布局,用来放置图片和文字,设置id,可以任意在Java代码中设置
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- android:minHeight="80dp"--> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/shape_toast" android:minWidth="120dp" android:gravity="center" android:orientation="vertical" android:padding="5dp"> <!--android:background="@drawable/toast_bg"--> <ImageView android:id="@+id/toast_image" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:layout_margin="2dp" android:background="@mipmap/pd_ic_finish"/> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:layout_gravity="center" android:text="保存成功" android:textColor="#ffffff" android:textSize="15dp"/> </LinearLayout> </LinearLayout>
关于Android中怎么使用Toast自定义图片和文字问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。