Toast。LENGTH_SHORT)。show();
}
});
}
@Override
public void onClick(View v) {
Toast toast = null;
switch (v。getId()) {
case R。id。btnSimpleToast:
Toast。makeText(getApplicationContext(), “默认Toast样式”,
Toast。LENGTH_SHORT)。show();
break;
case R。id。btnSimpleToastWithCustomPosition:
toast = Toast。makeText(getApplicationContext(),
“自定义位置Toast”, Toast。LENGTH_LONG);
toast。setGravity(Gravity。CENTER, 0, 0);
toast。show();
break;
case R。id。btnSimpleToastWithImage:
toast = Toast。makeText(getApplicationContext(),
“带图片的Toast”, Toast。LENGTH_LONG);
toast。setGravity(Gravity。CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast。getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject。setImageResource(R。drawable。icon);
toastView。addView(imageCodeProject, 0);
toast。show();
break;
case R。id。btnCustomToast:
LayoutInflater inflater = 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();
break;
case R。id。btnRunToastFromOtherThread:
new Thread(new Runnable() {
public void run() {
showToast();
}
})。start();
break;
}
}
}
2.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” android:padding=“5dip” android:gravity=“center”>
<Button android:layout_height=“wrap_content”
android:layout_width=“fill_parent” android:id=“@+id/btnSimpleToast”
android:text=“默认”>
<Button android:layout_height=“wrap_content”
android:layout_width=“fill_parent” android:text=“自定义显示位置”
android:id=“@+id/btnSimpleToastWithCustomPosition”>
<Button android:layout_height=“wrap_content”
android:layout_width=“fill_parent” android:id=“@+id/btnSimpleToastWithImage”
android:text=“带图片”>
<Button android:layout_height=“wrap_content”
android:layout_width=“fill_parent” android:text=“完全自定义”
android:id=“@+id/btnCustomToast”>
<Button android:layout_height=“wrap_content”
android:layout_width=“fill_parent” android:text=“其他线程”
android:id=“@+id/btnRunToastFromOtherThread”>
3.custom.xml
<?xml version=“1。0” encoding=“utf-8”?>
<LinearLayout
xmlns:android=“http://schemas。android。com/apk/res/android”
android:layout_height=“wrap_content” android:layout_width=“wrap_content”
android:background=“#ffffffff” android:orientation=“vertical”
android:id=“@+id/llToast” >
<TextView
android:layout_height=“wrap_content”
android:layout_margin=“1dip”
android:textColor=“#ffffffff”
android:layout_width=“fill_parent”
android:gravity=“center”
android:background=“#bb000000”
android:id=“@+id/tvTitleToast” />
<LinearLayout
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:id=“@+id/llToastContent”
android:layout_marginLeft=“1dip”
android:layout_marginRight=“1dip”
android:layout_marginBottom=“1dip”
android:layout_width=“wrap_content”
android:padding=“15dip”
android:background=“#44000000” >
<ImageView
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:id=“@+id/tvImageToast” />
<TextView
android:layout_height=“wrap_content”
android:paddingRight=“10dip”
android:paddingLeft=“10dip”
android:layout_width=“wrap_content”
android:gravity=“center”
android:textColor=“#ff000000”
android:id=“@+id/tvTextToast” />
原文链接:[http://www.eoeandroid.com/thread-187288-1-1.html]( )
本文转自 [https://blog.youkuaiyun.com/u011352918/article/details/9354917]( ),如有侵权,请联系删除。
## 最后
这里我特地整理了一份《**Android开发核心知识点笔记**》,里面就包含了自定义View相关的内容

除了这份笔记,还给大家分享 **Android学习PDF+架构视频+面试文档+源码笔记**,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。

分享上面这些资源,希望可以帮助到大家提升进阶,**如果你觉得还算有用的话,不妨把它们推荐给你的朋友~**
> 喜欢本文的话,给我点个小赞、评论区留言或者转发支持一下呗~
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618156601)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
B-1714464094696)]
除了这份笔记,还给大家分享 **Android学习PDF+架构视频+面试文档+源码笔记**,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。
[外链图片转存中...(img-3aah7ABp-1714464094697)]
分享上面这些资源,希望可以帮助到大家提升进阶,**如果你觉得还算有用的话,不妨把它们推荐给你的朋友~**
> 喜欢本文的话,给我点个小赞、评论区留言或者转发支持一下呗~
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618156601)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**