您的位置:首页 > 移动开发 > Android开发

Android自己定义Toast

2017-06-14 20:48 387 查看

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



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

/**
* 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;
}

凝视写着不过个包括了一个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代码

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;
}
}

上面有一句话要注意:

Construct an empty Toast object.  You must call {@link #setView} before you
* can call {@link #show}


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

public static Toast makeText(Context context,int imgId,CharSequence hintText,boolean showFailure,int duration)
參数解释:
context:上下文
imgId:显示图片资源
hintText:显示提示信息
showFailure:是否显示错误信息
duration:显示时长

调用Toast:

<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();
}

显示效果:





That's all

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: