Android(安卓)Toast 总结
16lz
2021-01-25
转载别人的写的不错,自己添加点注意事项,号称有5种效果。
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);
- LinearLayouttoastView=(LinearLayout)toast.getView();
- ImageViewimageCodeProject=newImageView(getApplicationContext());
- imageCodeProject.setImageResource(R.drawable.icon);
- toastView.addView(imageCodeProject,0);
- toast.show();
4.完全自定义效果
代码
- LayoutInflaterinflater=getLayoutInflater();
- Viewlayout=inflater.inflate(R.layout.custom,
- (ViewGroup)findViewById(R.id.llToast));
- ImageViewimage=(ImageView)layout
- .findViewById(R.id.tvImageToast);
- image.setImageResource(R.drawable.icon);
- TextViewtitle=(TextView)layout.findViewById(R.id.tvTitleToast);
- title.setText("Attention");
- TextViewtext=(TextView)layout.findViewById(R.id.tvTextToast);
- text.setText("完全自定义Toast");
- toast=newToast(getApplicationContext());
- toast.setGravity(Gravity.RIGHT|Gravity.TOP,12,40);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);
- toast.show();
5.其他线程
代码
- newThread(newRunnable(){
- publicvoidrun(){
- showToast();
- }
- }).start();
完整代码
1.Main,java
- packagecom.wjq.toast;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.view.Gravity;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.view.View.OnClickListener;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importandroid.widget.TextView;
- importandroid.widget.Toast;
- publicclassMainextendsActivityimplementsOnClickListener{
- Handlerhandler=newHandler();
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViewById(R.id.btnSimpleToast).setOnClickListener(this);
- findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
- this);
- findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
- findViewById(R.id.btnCustomToast).setOnClickListener(this);
- findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
- }
- publicvoidshowToast(){
- handler.post(newRunnable(){
- @Override
- publicvoidrun(){
- Toast.makeText(getApplicationContext(),"我来自其他线程!",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- publicvoidonClick(Viewv){
- Toasttoast=null;
- switch(v.getId()){
- caseR.id.btnSimpleToast:
- Toast.makeText(getApplicationContext(),"默认Toast样式",
- Toast.LENGTH_SHORT).show();
- break;
- caseR.id.btnSimpleToastWithCustomPosition:
- toast=Toast.makeText(getApplicationContext(),
- "自定义位置Toast",Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER,0,0);
- toast.show();
- break;
- caseR.id.btnSimpleToastWithImage:
- toast=Toast.makeText(getApplicationContext(),
- "带图片的Toast",Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER,0,0);
- LinearLayouttoastView=(LinearLayout)toast.getView();
- ImageViewimageCodeProject=newImageView(getApplicationContext());
- imageCodeProject.setImageResource(R.drawable.icon);
- toastView.addView(imageCodeProject,0);
- toast.show();
- break;
- caseR.id.btnCustomToast:
- LayoutInflaterinflater=getLayoutInflater();
- Viewlayout=inflater.inflate(R.layout.custom,
- (ViewGroup)findViewById(R.id.llToast));
- ImageViewimage=(ImageView)layout
- .findViewById(R.id.tvImageToast);
- image.setImageResource(R.drawable.icon);
- TextViewtitle=(TextView)layout.findViewById(R.id.tvTitleToast);
- title.setText("Attention");
- TextViewtext=(TextView)layout.findViewById(R.id.tvTextToast);
- text.setText("完全自定义Toast");
- toast=newToast(getApplicationContext());
- toast.setGravity(Gravity.RIGHT|Gravity.TOP,12,40);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);
- toast.show();
- break;
- caseR.id.btnRunToastFromOtherThread:
- newThread(newRunnable(){
- publicvoidrun(){
- showToast();
- }
- }).start();
- break;
- }
- }
- }
2.main,xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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">
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:id="@+id/btnSimpleToast"
- android:text="默认"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="自定义显示位置"
- android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:id="@+id/btnSimpleToastWithImage"
- android:text="带图片"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="完全自定义"
- android:id="@+id/btnCustomToast"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:text="其他线程"
- android:id="@+id/btnRunToastFromOtherThread"></Button>
- </LinearLayout>
3.custom.xml
- <?xmlversion="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"/>
- </LinearLayout>
- </LinearLayout>
注意: 使用toast = new Toast(context); 一定要先 toast.setView() ,然后才能toast.Show() 不然会出错。同时,我使用
LinearLayout toastView = (LinearLayout) toast.getView(); 然后再在这个LinearLayout种添加TextView会出错,后来就直接定义一个LinearLayout了。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android(安卓)RadioButton+Viewpager+Fragment实现底部导航栏
- Android应用自动更新功能的实现!!!软件更新,自动下载,安装
- Android(安卓)textview 不使用ScrollView也可滚动的方法
- android文件上传示例分享(android图片上传)
- 【原创】android中实现底部tabhost
- Android(安卓)listview里面包含checkbox
- android 问题汇总系列之四
- Android开发应用异步检查更新代码