今天在写PDA的时候用到了自定义的Toast比较经典于是记录一下


首先看一下android的Toast的源码发现:

 <h2>    /**     * Make a standard toast that just contains a text view.     *     * @param context  The context to use.  Usually your {@link android.app.Application}     *                 or {@link android.app.Activity} object.     * @param text     The text to show.  Can be formatted text.     * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or     *                 {@link #LENGTH_LONG}     *     */    public static Toast makeText(Context context, CharSequence text, int duration) {        Toast result = new Toast(context);        LayoutInflater inflate = (LayoutInflater)                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);        tv.setText(text);                result.mNextView = v;        result.mDuration = duration;        return result;    }</h2>

注释写着仅仅是个包含了一个TextView的Toast

不难发现这个Toast用了一个系统自带的layout去展示Toast的布局,然后用了result.mNextView 跟 result.mDuration把Toast跟传递的参数和显示的布局聚合起来,知道了原理我们就可以自定义Toast了

1,首先我们要自定一个布局来显示我们想要Toast显示的效果

2,通过传递参数跟我们自定义布局里面的组件进行交互

3,通过setView把我们的布局跟我们的Toast联系起来

<span style="font-size:18px;"><?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:background="@drawable/toast_shape"    >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal"        android:gravity="center"        >        <ImageView            android:id="@+id/iv_hint_icon"            android:layout_width="35dp"            android:layout_height="35dp"            android:src="@drawable/pda_failure_icon"            android:layout_marginLeft="5dp"            />        <TextView            android:id="@+id/tv_hint_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textStyle="bold"            android:textSize="28sp"            android:textColor="#eb4e4e"            android:layout_marginLeft="5dp"            android:text="上架失败 !"            android:layout_toRightOf="@+id/iv_hint_icon"            />        <TextView            android:id="@+id/tv_check_add_info"            android:layout_below="@+id/tv_hint_text"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textStyle="bold"            android:textSize="22sp"            android:textColor="#eb4e4e"            android:layout_marginTop="20dp"            android:layout_marginLeft="5dp"            android:text="请仔细核对上架信息!"            />    </RelativeLayout></LinearLayout></span>

接下来是定义的shape代码

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="10dp" />    <solid android:color="#ffffff" />    <stroke android:color="#000000" android:width="2dp"/>    <padding android:bottom="30dp" android:left="30dp" android:right="30dp" android:top="30dp" /></shape></span>

自定义Toast代码

<span style="font-size:18px;">public class PDAToast extends Toast{    /**     * Construct an empty Toast object.  You must call {@link #setView} before you     * can call {@link #show}.     *     * @param context The context to use.  Usually your {@link Application}     *                or {@link Activity} object.     */    public PDAToast(Context context) {        super(context);    }    public static Toast makeText(Context context,int imgId,CharSequence hintText,boolean showFailure,int duration){        Toast mToast = new Toast(context);        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View layout = mInflater.inflate(R.layout.pda_toast_layout,null);//TODO 设置对话框的布局        ImageView hint_icon = (ImageView) layout.findViewById(R.id.iv_hint_icon);        hint_icon.setImageResource(imgId);        TextView hint = (TextView) layout.findViewById(R.id.tv_hint_text);        hint.setText(hintText);        TextView failure_desc = (TextView) layout.findViewById(R.id.tv_check_add_info);        if(showFailure){            failure_desc.setVisibility(View.VISIBLE);        }else{            failure_desc.setVisibility(View.GONE);        }        mToast.setView(layout);        mToast.setGravity(Gravity.CENTER,0,0);        mToast.setDuration(duration);        return mToast;    }}</span>

上面有一句话要注意:

<span style="font-size:18px;">Construct an empty Toast object.  You must call {@link #setView} before you     * can call {@link #show}.</span>

提示构造一个空的Toast的话,在你show之前必须调用SetView否则会报错

<span style="font-size:18px;">  public static Toast makeText(Context context,int imgId,CharSequence hintText,boolean showFailure,int duration)</span>
<span style="font-size: 18px;"> 参数解释:</span>
<span style="font-size: 18px;"><span style="white-space:pre"></span>context:上下文</span>
<span style="font-size: 18px;"><span style="white-space:pre"></span>imgId:显示图片资源</span>
<span style="font-size: 18px;"><span style="white-space:pre"></span>hintText:显示提示信息</span>
<span style="font-size: 18px;"><span style="white-space:pre"></span>showFailure:是否显示错误信息</span>
<span style="font-size: 18px;"><span style="white-space:pre"></span>duration:显示时长</span>

调用Toast:

 <span style="font-size:18px;"><span style="white-space:pre"></span>    @Override            public void onSuccess(BaseResponseData dataObj, String jsonString) {                PDAToast.makeText(AddActivity.this,R.drawable.pda_success_icon,"上架成功 !",false,Toast.LENGTH_SHORT).show();            }            @Override            public void onFailure(int resCode, String msg, String jsonString) {                Log.e("TAG","result:"+resCode +" error desc :"+msg +" response data:"+jsonString);                PDAToast.makeText(AddActivity.this,R.drawable.pda_failure_icon,"上架失败 !",true,Toast.LENGTH_SHORT).show();            }</span>

显示效果:

Android自定义Toast_第1张图片

That's all

更多相关文章

  1. Android Studio 打Jar包和混淆代码。
  2. Android 代码模拟输入按键
  3. Android 获取手势onfling代码片段
  4. Android Studio新建布局XML, preview不显示问题解决
  5. android —— 常用代码utils
  6. Android中打开扬声器关闭麦克风的代码实现

随机推荐

  1. MySql5.x升级MySql8.x的方法步骤
  2. mysql存储过程之返回多个值的方法示例
  3. MySql Installer 8.0.18可视化安装教程图
  4. mysql存储过程之创建(CREATE PROCEDURE)和
  5. mysql存储过程之引发存储过程中的错误条
  6. Linux下安装MySQL8.0.11的教程
  7. CentOS7下 MySQL定时自动备份的实现方法
  8. mysql 8.0.18各版本安装及安装中出现的问
  9. Windows下mysql-5.7.28下载、安装、配置
  10. Linux下修改MySQL数据库数据文件路径的步