完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明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.其他线程 代码 new Thread(newRunnable() { public void run() { 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; public class Mainextends Activity implements OnClickListener { Handlerhandler = new Handler(); @Override publicvoid onCreate(Bundle savedInstanceState) { 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); } public voidshowToast() { handler.post(newRunnable() { @Override publicvoid run() { Toast.makeText(getApplicationContext(),"我来自其他线程!", Toast.LENGTH_SHORT).show(); } }); } @Override publicvoid onClick(View v) { 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 = new ImageView(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= new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT| Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); break; caseR.id.btnRunToastFromOtherThread: newThread(new Runnable() { publicvoid run() { showToast(); } }).start(); break; } } } 2.main,xml android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"android:padding="5dip" android:gravity="center"> android:text="默认"> android:id="@+id/btnSimpleToastWithCustomPosition"> android:text="带图片"> android:id="@+id/btnCustomToast"> android:id="@+id/btnRunToastFromOtherThread"> 3.custom.xml android:layout_height="wrap_content"android:layout_width="wrap_content" android:background="#ffffffff"android:orientation="vertical" android:id="@+id/llToast"> android:layout_margin="1dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:gravity="center" android:background="#bb000000" android:id="@+id/tvTitleToast"/> 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"> android:layout_gravity="center" android:layout_width="wrap_content" android:id="@+id/tvImageToast"/> android:paddingRight="10dip" android:paddingLeft="10dip" android:layout_width="wrap_content" android:gravity="center" android:textColor="#ff000000" android:id="@+id/tvTextToast"/> |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
物联网工程师必备:怎么选择不同的无线连接技术,本指南帮你忙!
3293 浏览 1 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】WIFI功能测试
3946 浏览 0 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】Arduino的替代SublimeText3+STino
3449 浏览 0 评论
使用端口扩展器轻松高效地向IIoT端点添加具有成本效益的子节点
4016 浏览 1 评论
20691 浏览 11 评论
模组有时候复位重启后输出日志为“REBOOT_CAUSE_SECURITY_PMU_POWER_ON_RESET”的原因?
812浏览 2评论
1041浏览 2评论
1042浏览 1评论
1154浏览 1评论
387浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 09:07 , Processed in 0.773014 second(s), Total 68, Slave 51 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (威廉希尔官方网站 图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号