转于:http://blog.csdn.net/jackhenry/article/details/7340278
------------------------------------------------------------------------------------
在Android开发过程中,相信大家对Toast都很熟悉,在一定程度上它可以提高用户体验,比如用户登录浏览器时,浏览器肯定会检测当前设备的网络连接状况,如果当前设备的网络连接是断开的,就可以使用Toast告诉用户,网络初始化失败!让用户检测是否打开数据开关或连接Wifi,如图:
但是这种样式的显示有时候不能满足我们的需求,而且看起来也比较不美观。
然后本人就想重写写个不一样的Toast,再查看了Android的源码后,发现非常简单。
Android源码Toast.java部分代码:
- 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;
- }
-----------------------------------------^-^......直接上代码
新的Toast的layout文件 newtoast.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:gravity="center_vertical"
- android:background="@drawable/btn_bg"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/image0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- />
- <TextView
- android:id="@+id/text0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:textSize="10sp"
- android:textColor="#000"
- />
- </LinearLayout>
新Toast背景 btn_bg.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android">
- <corners
- android:radius="6dp"
- />
- <padding
- android:left="5dp"
- android:top="5dp"
- android:right="5dp"
- android:bottom="5dp"
- />
- <solid
- android:color="#FFFFFF"
- />
- </shape>
NewToast.java:
- public class NewToast extends Toast {
- public NewToast(Context context) {
- super(context);
- }
- public static Toast makeText(Context context, int resId, CharSequence text, int duration) {
- Toast result = new Toast(context);
- //获取LayoutInflater对象
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- //由layout文件创建一个View对象
- View layout = inflater.inflate(R.layout.newtoast, null);
- //实例化ImageView和TextView对象
- ImageView imageView = (ImageView) layout.findViewById(R.id.image0);
- TextView textView = (TextView) layout.findViewById(R.id.text0);
- imageView.setImageResource(resId);
- textView.setText(text);
- result.setView(layout);
- result.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- result.setDuration(duration);
- return result;
- }
- }
main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/btn0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/ic_btn_name"
- android:background="@drawable/btn_bg"
- />
- </LinearLayout>
strings.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">NewToastDemo</string>
- <string name="ic_btn_name">测试按钮</string>
- </resources>
最后来测试一下,MainActivity.java:
- public class MainActivity extends Activity implements OnClickListener {
- //声明一个Button对象
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button) findViewById(R.id.btn0);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- NewToast.makeText(MainActivity.this, R.drawable.prettygirl, "这是一个自定义的Toast!", Toast.LENGTH_SHORT).show();
- }
- }
run app之后,点击按钮,出现如下图所示:
这样一个新的Toast就写好了。
相关推荐
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
Android应用源码之自定义彩色Toast.zip
Android应用源码之自定义彩色Toast.zip项目安卓应用源码下载Android应用源码之自定义彩色Toast.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android例子源码自定义彩色Toast
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
android利用布局文件自定义Dialog、Toast
android Toast对象的使用 自定义Toast
自己整理的自定义控件方法,看了你就会做自定义Dialog和Toast了,没有多余代码,很简洁。背景、按钮都能自定义
Android:实现Toast自定义样式(包括:自定义位置、带图片等)、自定义显示时长(包括可用系统时长、可自定义)
android自定义时长Toast,Toast 显示时间可以自己设定
Android Toast 自定义背景、图片 随心使用
/* 显示toast,自己定义显示长短。 * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time length long类型,我们传入的时间长度(如500)*/
默认Toast(大家都会),自定义Toast位置,自定义Toast样式
自定义Toast,设置Toast显示位置,自定义Toast的复杂布局 博客地址:http://blog.csdn.net/xy_nyle/article/details/20137953
Android中实现带声音提示的Toast (自定义扩展Toast)
自定义Android Toast Demo
Android中自定义Toast.pdf 学习资料 复习资料 教学资源
安卓开发-自定义彩色Toast
android 自定义toast,toast自定义显示时间,toast自定义显示样式