Android中Toast的几种使用方法
答案:3 悬赏:70 手机版
解决时间 2021-02-10 04:11
- 提问者网友:温柔港
- 2021-02-09 07:12
Android中Toast的几种使用方法
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-02-09 07:34
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。有几种使用方式:
1、默认效果 代码
Toast.makeText(getApplicationContext(),"默认Toast样式",Toast.LENGTH_SHORT).show();
2、自定义显示位置效果 代码
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3、带图片效果 代码
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView imageCodeProject = newImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4、完全自定义效果
代码
LayoutInflaterinflater = getLayoutInflater();
View layout =inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP,12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他线程
代码
newThread(new Runnable() {
public void run() {
showToast();
}
}).start();
1、默认效果 代码
Toast.makeText(getApplicationContext(),"默认Toast样式",Toast.LENGTH_SHORT).show();
2、自定义显示位置效果 代码
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3、带图片效果 代码
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView imageCodeProject = newImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4、完全自定义效果
代码
LayoutInflaterinflater = getLayoutInflater();
View layout =inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP,12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他线程
代码
newThread(new Runnable() {
public void run() {
showToast();
}
}).start();
全部回答
- 1楼网友:詩光轨車
- 2021-02-09 08:48
我现在的设置是居中靠顶
- 2楼网友:北方的南先生
- 2021-02-09 08:36
toast现在也是可以全局调用啊
我这里封装了一个toast
这个解决了toast调用多次,屏幕上一直弹出的问题
调用方式
mytoast.showtoast(context,"提示",toast.length_long);
那么你可以写个公共的activity父类,让所有的子类都继承它
这样你可以再封装了下
public void showlongtoast(string msg){
mytoast.showtoast(this,msg,toast.length_long);
}
public void showshorttoast(string msg){
mytoast.showtoast(this,msg,toast.length_short);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯