自定义Toast(图片+文字+全屏)

xiaoxiao2021-02-28  64

灵感来自这篇博客,只是需要的样式和他的有点出入:http://blog.csdn.net/zzs761120751/article/details/49446045

这篇文章也不错,如果不需要那么复杂的可以直接在Toast中加入View,不必自己写布局:http://thunder-yan.iteye.com/blog/1558292

上代码:

public class CustomToast { public static void ToastWithImageShort(Context context, int imageRes, CharSequence text){ //创建一个Toast提示消息 Toast toast = Toast.makeText(context,null, Toast.LENGTH_SHORT); //设置Toast提示消息在屏幕上的位置 toast.setGravity(Gravity.FILL, 0, 0); //必须要FILL,否则无法显示全屏 //TODO 自定义的图片和文字 View view = LinearLayout.inflate(context, R.layout.layout_dormitory_submit_check_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_custom); TextView textView = (TextView)view.findViewById(R.id.tv_custom); if(imageRes != 0) imageView.setImageDrawable(context.getResources().getDrawable(imageRes)); textView.setText(text); //创建一个LineLayout容器 LinearLayout toastView = (LinearLayout) toast.getView(); LinearLayout.LayoutParams vlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); vlp.setMargins(0,0,0,0); view.setLayoutParams(vlp); toastView.setBackgroundColor(context.getResources().getColor(R.color.transparent_slight)); toastView.addView(view); toast.show(); } public static void ToastWithImageLong(Context context, int imageRes, CharSequence text){ //创建一个Toast提示消息 Toast toast = Toast.makeText(context,null, Toast.LENGTH_LONG); //设置Toast提示消息在屏幕上的位置 toast.setGravity(Gravity.FILL, 0, 0); //TODO 自定义的图片和文字 View view = LinearLayout.inflate(context, R.layout.layout_dormitory_submit_check_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.iv_custom); TextView textView = (TextView)view.findViewById(R.id.tv_custom); if(imageRes != 0) imageView.setImageDrawable(context.getResources().getDrawable(imageRes)); textView.setText(text); //创建一个LineLayout容器 LinearLayout toastView = (LinearLayout) toast.getView(); LinearLayout.LayoutParams vlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); vlp.setMargins(0,0,0,0); view.setLayoutParams(vlp); toastView.setBackgroundColor(context.getResources().getColor(R.color.transparent_slight)); toastView.addView(view); toast.show(); } } 注意这里一定要用 toast.setGravity(Gravity.FILL, 0, 0);否则将无法全屏显示。

xml如下:R.layout.layout_dormitory_submit_check_view

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="50dp" android:paddingRight="50dp" android:orientation="vertical" android:background="@drawable/submit_check_shape" android:gravity="center"> <ImageView android:id="@+id/iv_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_submit_check" android:layout_marginTop="20dp"/> <TextView android:id="@+id/tv_custom" android:layout_marginTop="10dp" android:layout_marginBottom="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" android:textSize="18dp" android:textColor="@color/white_ffffff"/> </LinearLayout> </LinearLayout> 可以自己设计View传入,这里就不说那么多了,这个效果是带外围阴影和图片的Toast,效果图展示在评论咯。

转载请注明原文地址: https://www.6miu.com/read-81347.html

最新回复(0)