中文域名解析网站设计相关的网站
2026/4/5 13:23:27 网站建设 项目流程
中文域名解析网站,设计相关的网站,百度免费推广网站,wordpress添加宋体字库2.5.7 Toast(吐司)的基本使用分类 Android 基础入门教程本节引言#xff1a;好的#xff0c;终于学习完Adapter类相关的一些控件#xff0c;当然除了讲解的那几个#xff0c;还有其他很多的 相关的控件#xff0c;就不慢慢讲解了~有需要的自行查阅文档#xff0c;查看相关…2.5.7 Toast(吐司)的基本使用分类Android 基础入门教程本节引言好的终于学习完Adapter类相关的一些控件当然除了讲解的那几个还有其他很多的 相关的控件就不慢慢讲解了~有需要的自行查阅文档查看相关的用法本节带来的是 Android用于提示信息的一个控件——Toast(吐司)Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失 非常常用本节我们就来学习Toast的使用1.直接调用Toast类的makeText()方法创建这是我们用的最多的一种形式了比如点击一个按钮然后弹出Toast用法Toast.makeText(MainActivity.this, 提示的内容, Toast.LENGTH_LONG).show();第一个是上下文对象对二个是显示的内容第三个是显示的时间只有LONG和SHORT两种 会生效即时你定义了其他的值最后调用的还是这两个另外Toast是非常常用的我们可以把这些公共的部分抽取出来写到一个方法里 需要显示Toast的时候直接调用这个方法就可以显示Toast这样方便很多 示例如下void midToast(String str, int showTime) { Toast toast Toast.makeText(global_context, str, showTime); toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0); //设置显示位置 TextView v (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.YELLOW); //设置字体颜色 toast.show(); }上面这个抽取出来的方法我们发现我们可以调用setGravity设置Toast显示的位置以及获得 通过findViewById(android.R.id.message)获得显示的文本然后进行设置颜色或者大小等 这就是第二种通过构造方法来定制Toast!2.通过构造方法来定制Toast上面定制了文本以及显示位置下面我们写两个简单的例子1.定义一个带有图片的Toast效果图关键代码private void midToast(String str, int showTime) { Toast toast Toast.makeText(mContext, str, showTime); toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //设置显示位置 LinearLayout layout (LinearLayout) toast.getView(); layout.setBackgroundColor(Color.BLUE); ImageView image new ImageView(this); image.setImageResource(R.mipmap.ic_icon_qitao); layout.addView(image, 0); TextView v (TextView) toast.getView().findViewById(android.R.id.message); v.setTextColor(Color.YELLOW); //设置字体颜色 toast.show(); }2.Toast完全自定义如果上面的那种还满足不了你的话那么你完全可以自己写一个Toast的布局然后显示出来 但是时间我们依旧控制不了运行效果图关键代码private void midToast(String str, int showTime) { LayoutInflater inflater getLayoutInflater(); View view inflater.inflate(R.layout.view_toast_custom, (ViewGroup) findViewById(R.id.lly_toast)); ImageView img_logo (ImageView) view.findViewById(R.id.img_logo); TextView tv_msg (TextView) view.findViewById(R.id.tv_msg); tv_msg.setText(str); Toast toast new Toast(mContext); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(view); toast.show(); }还有自定义Toast的布局以及圆角背景圆角背景bg_toast.xml?xml version1.0 encodingutf-8? shape xmlns:androidhttp://schemas.android.com/apk/res/android !-- 设置透明背景色 -- solid android:color#BADB66 / !-- 设置一个黑色边框 -- stroke android:width1px android:color#FFFFFF / !-- 设置四个圆角的半径 -- corners android:bottomLeftRadius50px android:bottomRightRadius50px android:topLeftRadius50px android:topRightRadius50px / !-- 设置一下边距,让空间大一点 -- padding android:bottom5dp android:left5dp android:right5dp android:top5dp / /shape布局文件view_toast_custom.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:idid/lly_toast android:layout_widthmatch_parent android:layout_heightmatch_parent android:backgrounddrawable/bg_toast android:orientationhorizontal ImageView android:idid/img_logo android:layout_width24dp android:layout_height24dp android:layout_marginLeft10dp android:srcmipmap/iv_lol_icon1 / TextView android:idid/tv_msg android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_marginLeft10dp android:textSize20sp / /LinearLayout非常简单嘿嘿~

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询