Android自定义控件之自定义Toast
来源:互联网 发布:网络舆情工作通知 编辑:程序博客网 时间:2024/05/22 03:29
在实际开发中经常会给用户弹出相应的通知信息,可能有的弹出通知的界面会跟系统自带的界面有很大的差别,这个时候就需要开发者自己定义Toast的布局了
我在项目中也遇到了自定义Toast的需求,所以就将他封装了起来,方便以后继续使用
下面贴出的是自定义Toast的布局,可根据实际的要求更换布局
<?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="wrap_content" android:background="@drawable/bg_toast" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="10dp" android:paddingTop="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:gravity="center_horizontal" android:orientation="horizontal" > <ImageView android:id="@+id/ivStat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toast_sucess_icon" /> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:text="标题" android:textColor="#CCCCCC" android:textSize="17dp" /> </LinearLayout> <TextView android:id="@+id/tvContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="10dp" android:gravity="center" android:text="内容" android:textColor="#B2B2B2" android:textSize="15dp" /></LinearLayout>
由于我在项目中需要弹出好几个不同样式的Toast,但大致布局都是一样的,只是里面的文字或者图片不同,所以在封装的时候里面会有多个弹出Toast的方法,在实际开发中可依据项目需求自己定义
package com.renrui.job.util;import com.renrui.job.R;import android.content.Context;import android.text.TextUtils;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;/** * */public class CustomToast{private static Toast myToast;private static View viewMyToast;private static ImageView ivStat;private static TextView tvTitle;private static TextView tvContent;private static Toast myToastIntegral;private static View viewMyToastIntegral;private static TextView tvIntegral;private static TextView tvIntegralContent;private static Toast getMyToast(Context mContext, String title, String content){if (myToast == null){myToast = new Toast(mContext);viewMyToast = View.inflate(mContext, R.layout.view_mytoast, null);ivStat = (ImageView) viewMyToast.findViewById(R.id.ivStat);tvTitle = (TextView) viewMyToast.findViewById(R.id.tvTitle);tvContent = (TextView) viewMyToast.findViewById(R.id.tvContent);}// 标题,不会为空if (tvTitle.getVisibility() != View.VISIBLE){tvTitle.setVisibility(View.VISIBLE);}tvTitle.setText(title);// 内容有可能为空if (TextUtils.isEmpty(content)){if (tvContent.getVisibility() != View.GONE){tvContent.setVisibility(View.GONE);}} else{if (tvContent.getVisibility() != View.VISIBLE){tvContent.setVisibility(View.VISIBLE);}tvContent.setText(content);}myToast.setView(viewMyToast);return myToast;}/** * 提示成功信息 * * @param mContext * @param title * 标题,不能为空 * @param content * 内容可以为空 */public static void makeTextSucess(Context mContext, String title, String content){myToast = CustomToast.getMyToast(mContext, title, content);ivStat.setBackgroundResource(R.drawable.toast_sucess_icon);myToast.setDuration(1000);myToast.show();}public static void makeTextSucess(Context mContext, String title, String content, int duration){myToast = CustomToast.getMyToast(mContext, title, content);ivStat.setBackgroundResource(R.drawable.toast_sucess_icon);myToast.setDuration(duration);myToast.show();}/** * 提示失败信息 */public static void makeTextError(Context mContext, String title, String content){myToast = CustomToast.getMyToast(mContext, title, content);ivStat.setBackgroundResource(R.drawable.toast_error_icon);myToast.setDuration(1000);myToast.show();}/** * 提示警告信息 */public static void makeTextWarn(Context mContext, String title, String content){myToast = CustomToast.getMyToast(mContext, title, content);ivStat.setBackgroundResource(R.drawable.toast_warn_icon);myToast.setDuration(1000);myToast.show();}/** * 增加积分信息 * * @param mContext * @param integral * 积分 * @param content * 内容 */public static void makeTextAddIntegral(Context mContext, String integral, String content){if (myToastIntegral == null){myToastIntegral = new Toast(mContext);viewMyToastIntegral = View.inflate(mContext, R.layout.view_mytoast_integral, null);tvIntegral = (TextView) viewMyToastIntegral.findViewById(R.id.tvIntegral);tvIntegralContent = (TextView) viewMyToastIntegral.findViewById(R.id.tvIntegralContent);}// +分tvIntegral.setText("+ " + integral);// 内容有可能为空tvIntegralContent.setText(content);myToastIntegral.setView(viewMyToastIntegral);myToastIntegral.setDuration(1000);myToastIntegral.show();}}
0 0
- Android自定义控件之自定义Toast
- Android之自定义Toast
- 提示控件之自定义Toast
- 自定义控件:自定义Toast
- android之自定义Toast使用方法
- android开发之自定义toast
- Android之Toast自定义动画
- android应用程序中使用自定义Toast控件
- [android自定义控件]带有图片的Toast
- 微信小程序自定义控件--toast(仿Android)
- 自定义控件:Toast
- 【android自定义控件】自定义Toast,AlterDialog,Notification <四>
- iOS开发系列之常用自定义控件开发集—Android的Toast控件开发
- Android软件开发之 自定义别样Toast
- Android学习笔记之自定义Toast
- Android学习笔记之自定义Toast
- android开发基础之-----------自定义toast
- Android之Toast自定义Toastdialog对话框
- ActiveAndroid 管理数据库
- Spark与Hadoop关系
- CSS高级技巧
- 开源BI报表及OLAP多维分析平台OPENI(二)—搭建Eclipse下的Openi开发环境
- linux rz上传文件及出错解决方案
- Android自定义控件之自定义Toast
- 笔记-AsyncTask
- 实现可点击的幸运大转盘
- 获取图片中某点或区域颜色
- 最长公共子序列问题——LCS
- (5) php校验时间的合法性 && work attitude 20151020 ):
- 恢复平衡0x8b,0xff,0x55,0x8b,0xec
- 调研内容(算法相关--GDA,GMM)
- vc 使用odbc连接mysql数据库分页查询