Android Toast在开发中经常使用,多用于向用户展示提示信息,只不过系统的Toast有些太单调了,不够美观,下面介绍一种超简单的自定义Toast的方式:
在介绍自定义Toast之前,和大家一起看几行系统Toast的源码:
public static Toast makeText(Context context, CharSequence text, @Duration 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;
}
这是系统Toast的mekeText()方法,大家天天都在用,源码很简单:new 一个Toast对象,加载一个布局页面(一个简单的TextView),把要显示的内容设置到TextView上,方法返回该Toast对象。[就这么简单]
我们自定义Toast其实只想改变Toast的UI,让它更美观一些,甚至加一些我们的东西进去(图标之类的),核心代码就是makeText方法,通过修改这个方法完全可以随心所欲的定义我们的Toast。
下面说一下我自定义Toast的思路:
1.创建一个类Toast(类名与系统相同是为了我们的书写习惯考虑)
2.定义makeText方法(定义与系统相同的makeText方法,核心是换成我们自定义的布局)
就是这么简单!看完代码就明白了…
public class Toast {
public @interface Duration {
}
/**
* Show the view or text notification for a short period of time. This time
* could be user-definable. This is the default.
*/
public static final int LENGTH_SHORT = 0;
/**
* Show the view or text notification for a long period of time. This time
* could be user-definable.
*/
public static final int LENGTH_LONG = 1;
private 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 android.widget.Toast makeText(Context context, CharSequence text, @Duration int duration) {
android.widget.Toast result = new android.widget.Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.transient_notification, null);
TextView tv = (TextView) v.findViewById(R.id.message);
tv.setText(text);
result.setView(v);
result.setDuration(duration);
return result;
}
/**
* Make a standard toast that just contains a text view with the text from a resource.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
* @throws Resources.NotFoundException if the resource can't be found.
*/
public static android.widget.Toast makeText(Context context, @StringRes int resId, @Duration int duration)
throws Resources.NotFoundException {
return makeText(context, context.getResources().getText(resId), duration);
}
}
核心是makeText(Context context, CharSequence text, @Duration int duration)方法,仍然是创建系统Toast,关键点是加载我们自己的transient_notification.xml布局文件,细心的朋友可能还会看到:处理根布局View和duration两个变量,我们分别使用系统Toast提供的setView和setDuration两个public方法。
transient_notification.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/toast_frame"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="@mipmap/qq"
android:drawableRight="@mipmap/qq"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="#1bb5d7"/>
</LinearLayout>
需要什么样式的Toast,就定义自己的transient_notification.xml布局就可以,显示图片还是文字完全由你掌控!
我们的Toast定义完了,怎么用呢?
Toast.makeText(MainActivity.this, "自定义toast", Toast.LENGTH_LONG).show();
看到这行代码有没有很亲切!没错,我们的自定义Toast就是这么用的!
只有一点不同,不要再导入系统的Toast类了,用自己定义的,连包都不需要导入了!
import android.widget.Toast;//删掉这句,无需导包