自定义toast
来源:互联网 发布:逆战刷箱子淘宝 编辑:程序博客网 时间:2024/06/05 02:34
import android.app.Activity;import android.view.Gravity;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;/** * creator gonghaohao * class name:ToastUtil * created at 2017/10/16 16:32 * function describe:自定义toast弹框 * 位置居中,包含图片、文字,自定义背景 * modify */public class ToastUtil { public static void centerToast(final Activity context, String string) { RelativeLayout layout = (RelativeLayout) context.getLayoutInflater().inflate(R.layout.layout_custom_toast, null); (layout.findViewById(R.id.tvCheckoutWay)).setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher)); ((TextView) layout.findViewById(R.id.tvPercent)).setText(string); Toast toast = new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0);//居中 toast.setView(layout);//setting the view of custom toast layout toast.show(); }
public static void centerPicToast(final Activity context, String string,int pic) { RelativeLayout layout = (RelativeLayout) context.getLayoutInflater().inflate(R.layout.layout_custom_toast, null); (layout.findViewById(R.id.tvCheckoutWay)).setBackgroundDrawable(context.getResources().getDrawable(pic)); ((TextView) layout.findViewById(R.id.tvPercent)).setText(string); Toast toast = new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.setView(layout);//setting the view of custom toast layout toast.show(); }
}布局文件:layout_custom_toast
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0.5" android:background="@drawable/toast_bg"> <ImageView android:id="@+id/tvCheckoutWay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:background="@mipmap/ic_launcher"></ImageView> <TextView android:id="@+id/tvPercent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvCheckoutWay" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="输入有误" android:textColor="#aabbcc" android:textSize="20sp" /> </RelativeLayout></RelativeLayout>toast背景资源文件:toast_bg<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="8dp"></corners> <solid android:color="#000000"></solid></shape>使用:ToastUtil.centerToast(MainActivity.this, "我是自定义toast");ToastUtil.centerPicToast(MainActivity.this, "我是自定义toast",R.mipmap.ic_launcher);
效果图
阅读全文
0 0
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- Toast自定义
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 增删改查表格
- 画出典型的Observer设计模式的类图和时序图
- 图片的自动轮播
- centos7下安装mysql5.7.20
- 后端修改密码的简单弹窗
- 自定义toast
- Linux上nginx安装配置(配置虚拟主机,静态资源)
- Android swiperefreshlayout 实现上拉刷新 加载
- 点击头像,调用相机,相册改变头像
- 游戏内统一支付系统设计与实现
- Spark存储与读取文件方法小结
- Twemproxy+ssdb双主配置
- eXtremeDB内存数据库7.0新版本的强大功能加强了它在物联网的关键作用
- Java并发编程(四)未处理异常、线程池和ThreadLocal类