Android的各种Toast .
2012-09-22 09:06
375 查看
Toast仅仅是向用户做出一个提示,提示有各种各样的方式。下面将一一道来。
1.最简单的Toast
[java]
view plain
copy
print
?
Toast.makeText(this, "默认", Toast.LENGTH_SHORT).show();
2.自定义显示位置的Toast
[java]
view plain
copy
print
?
Toast t;
t=Toast.makeText(this, "自定义显示位置", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
3带图片的Toast
[java]
view plain
copy
print
?
t=Toast.makeText(this, "带图片的Toast", Toast.LENGTH_SHORT);
LinearLayout ll=(LinearLayout) t.getView();
ImageView iv=new ImageView(this);
iv.setBackgroundResource(R.drawable.ic_launcher);
ll.addView(iv,0);
t.show();
4.完全自定义的Toast
[java]
view plain
copy
print
?
t=new Toast(this);
LayoutInflater li=this.getLayoutInflater();
View vee=li.inflate(R.layout.iii, null);
t.setView(vee);
t.show();
5.来自其它线程的Toast
[java]
view plain
copy
print
?
Handler h=new Handler();
///////////////////////////////////////////////////
new Thread(){
@Override
public void run() {
showToast();
}
}.start();
////////////////////////////////////////////////////////
protected void showToast() {
h.post(new Runnable(){
@Override
public void run() {
Toast.makeText(getApplicationContext(), "come from other thread", Toast.LENGTH_SHORT).show();
}
});
}
来自其它线程的Toast我有一点要说明,h.post(Runnable r);这个方法里的run是在主线程的。
1.最简单的Toast
[java]
view plain
copy
?
Toast.makeText(this, "默认", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "默认", Toast.LENGTH_SHORT).show();
2.自定义显示位置的Toast
[java]
view plain
copy
?
Toast t;
t=Toast.makeText(this, "自定义显示位置", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
Toast t; t=Toast.makeText(this, "自定义显示位置", Toast.LENGTH_SHORT); t.setGravity(Gravity.CENTER, 0, 0); t.show();
3带图片的Toast
[java]
view plain
copy
?
t=Toast.makeText(this, "带图片的Toast", Toast.LENGTH_SHORT);
LinearLayout ll=(LinearLayout) t.getView();
ImageView iv=new ImageView(this);
iv.setBackgroundResource(R.drawable.ic_launcher);
ll.addView(iv,0);
t.show();
t=Toast.makeText(this, "带图片的Toast", Toast.LENGTH_SHORT); LinearLayout ll=(LinearLayout) t.getView(); ImageView iv=new ImageView(this); iv.setBackgroundResource(R.drawable.ic_launcher); ll.addView(iv,0); t.show();
4.完全自定义的Toast
[java]
view plain
copy
?
t=new Toast(this);
LayoutInflater li=this.getLayoutInflater();
View vee=li.inflate(R.layout.iii, null);
t.setView(vee);
t.show();
t=new Toast(this); LayoutInflater li=this.getLayoutInflater(); View vee=li.inflate(R.layout.iii, null); t.setView(vee); t.show();
5.来自其它线程的Toast
[java]
view plain
copy
?
Handler h=new Handler();
///////////////////////////////////////////////////
new Thread(){
@Override
public void run() {
showToast();
}
}.start();
////////////////////////////////////////////////////////
protected void showToast() {
h.post(new Runnable(){
@Override
public void run() {
Toast.makeText(getApplicationContext(), "come from other thread", Toast.LENGTH_SHORT).show();
}
});
}
Handler h=new Handler(); /////////////////////////////////////////////////// new Thread(){ @Override public void run() { showToast(); } }.start(); //////////////////////////////////////////////////////// protected void showToast() { h.post(new Runnable(){ @Override public void run() { Toast.makeText(getApplicationContext(), "come from other thread", Toast.LENGTH_SHORT).show(); } }); }
来自其它线程的Toast我有一点要说明,h.post(Runnable r);这个方法里的run是在主线程的。
相关文章推荐
- Android的各种Toast
- android从源码解析并实现各种Toast效果合集
- Android杂谈---各种Toast
- android从源码解析并实现各种Toast效果合集
- Android杂谈---各种Toast
- Android Toast用法详解(各种自定义Toast)
- android Toast 各种显示方式
- android应用开发详解(四)----------各种菜单(optionmenu、contextmenu、submenu)和Toast的使用
- Android Toast用法详解(各种自定义Toast)
- Toast大全(默认,顶部,底部,各种自定义) Android
- Android的各种通知Notification、Dialog、Toast、Snackbar
- Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析
- Android Context完全解析,你所不知道的Context的各种细节
- Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
- 一步一步学android控件(之七) —— Toast
- Android 中各种权限深入体验及详解
- Android之Toast简单实现不循环提示
- Android 各种技术片段,总有你不知道的(持续添加中)
- 关于Android4.x系统默认显示方向各种修改
- Android的各种数据存储方式_part1