Toast的基本使用
2016-03-24 16:45
267 查看
1.Toast方法封装
2.自定义Toast
1)自定义的Toast布局和圆角背景
bg_toast.xml:圆角背景
布局文件:view_toast_custom.xml:
2)Java代码
demo下载地址:
http://download.csdn.net/detail/kinglong68/9471563
public static Toast toast = null; public static void showToast(Context context, String msg) { if (toast == null) { toast = Toast.makeText(context, msg, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, -100); } else { toast.setText(msg); } toast.show(); }
2.自定义Toast
1)自定义的Toast布局和圆角背景
bg_toast.xml:圆角背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置透明背景色 --> <solid android:color="#BADB66" /> <!-- 设置一个黑色边框 --> <stroke android:width="1px" android:color="#FFFFFF" /> <!-- 设置四个圆角的半径 --> <corners android:bottomLeftRadius="50px" android:bottomRightRadius="50px" android:topLeftRadius="50px" android:topRightRadius="50px" /> <!-- 设置一下边距,让空间大一点 --> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape>
布局文件:view_toast_custom.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lly_toast" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_toast" android:orientation="horizontal"> <ImageView android:id="@+id/img_logo" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="10dp" android:src="@mipmap/iv_lol_icon1" /> <TextView android:id="@+id/tv_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="20sp" /> </LinearLayout>
2)Java代码
private void midToast(String str, int showTime) { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.view_toast_custom, (ViewGroup) findViewById(R.id.lly_toast)); ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo); TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg); tv_msg.setText(str); Toast toast = new Toast(mContext); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(view); toast.show(); }
demo下载地址:
http://download.csdn.net/detail/kinglong68/9471563
相关文章推荐
- oracle 自启动
- leetcode 15. 3Sum 二维vector
- BVH文件格式解析
- 【bzoj2631】【tree】【lct】
- vijos 1011 清帝之惑之顺治 (记忆化搜索)
- storm, kafka集成之本地开发、测试
- 万花筒 | 背完这444句,你的口语绝对不成问题了
- Ajax实战项目1 验证用户的唯一性 环境(Ajax+struts2)
- Java中的序列化Serialable高级详解
- iOS随机数生成
- Android开发_如何调用系统默认浏览器访问
- 通往架构师之路
- hadoop复合键排序使用方法
- https://github.com/akullpp/awesome-java
- 一个映射到mac风格按键的AHK脚本(替换虚拟机键盘映射)
- Apache配置基于域名的虚拟主机
- Struts2当调用action时出现HTTP Status 404 问题解决
- 7.自定义Dialog提示框
- C++文件操作详解http://www.cnblogs.com/likebeta/archive/2012/06/16/2551662.html
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍