首页 > 其他分享 >Toast的基本使用

Toast的基本使用

时间:2023-03-16 23:32:51浏览次数:25  
标签:Toast 基本 layout toast 使用 android id TextView

终于学习完Adapter类相关的一些控件,当然除了讲解的那几个,还有其他很多的相关的控件,就不慢慢讲解了~有需要的自行查阅文档,查看相关的用法,本节带来的是:Android用于提示信息的一个控件——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

效果图

Toast的基本使用_xml

关键代码

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的布局,然后显示出来; 但是时间我们依旧控制不了!

运行效果图

Toast的基本使用_android_02

关键代码

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 version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置透明背景色 -->
<solid android:color="#BADB66" />
<!-- 设置一个黑色边框 -->
<stroke
android:width="1px"
android:color="#FFFFFF" />
<!-- 设置四个圆角的半径 -->
<corners
android:bottomLeftRadius="50px"
android:bottomRightRadius="50px"
android:topLeftRadius="50px"
android:topRightRadius="50px" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>

布局文件:view_toast_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lly_toast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_toast"
android:orientation="horizontal">

<ImageView
android:id="@+id/img_logo"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:src="@mipmap/iv_lol_icon1" />

<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20sp" />

</LinearLayout>

标签:Toast,基本,layout,toast,使用,android,id,TextView
From: https://blog.51cto.com/u_15641375/6126053

相关文章

  • Unity 学习使用InputSystem接收键盘的输入
    目录快速实践配置InputAction创建场景编写代码理论学习ActionInputActionTypeInputBinding快速实践配置InputAction右键点击工程(project)面板空白处,弹出菜单栏,选择Cr......
  • 为什么需要消息队列?使用消息队列有什么好处?
    为什么需要消息队列?当系统中出现“生产“和“消费“的速度或稳定性等因素不一致的时候,就需要消息队列,作为抽象层,弥合双方的差异。消息被发送到队列中,“消息队列”是在消......
  • 人工智能python3+tensorflow人脸识别_使用 face-api.js 在你的浏览器中做人脸识别(基于
    我很兴奋地告诉你,终于可以在浏览器中运行人脸识别了!这篇文章我将介绍face-api.js,这个类库构建于tensorflow.js之上。它实现了多个CNNs(卷积神经网络)以解决人脸检测、......
  • 【Python】使用 multiprocessing.dummy 执行多线程任务
    1.#-*-coding:utf-8-*-2.#frommultiprocessingimportPool多进程3.frommultiprocessing.dummyimportPoolasThreadPool#多线程4.importtime5.im......
  • 使用旧电脑玩Linux
    今天给大家讲讲使用旧电脑玩Linux,大家应该都知道旧电脑的硬件一般比较落后,特别是一些非常老的电脑,目前还在使用的是机械硬盘,如是要跑windows可想而知,但是Linux系统对硬件性......
  • 设备树基本语法
    根节点子节点节点名称reg属性reg=<内容由#address-cells和#size-cells决定>控制的是子节点示例1示例2model属性status属性compatible属性......
  • 使用旧电脑玩Linux
    今天给大家讲讲使用旧电脑玩Linux,大家应该都知道旧电脑的硬件一般比较落后,特别是一些非常老的电脑,目前还在使用的是机械硬盘,如是要跑windows可想而知,但是Linux系统对硬件性......
  • HashMap为啥不能用基本类型作为key值
    java中,为什么基本类型不能做为HashMap的键值,而只能是引用类型,把引用类型作为HashMap的键值首先,HashMap存储数据的特点是:无序、无索引、不能存储重复元素。其次,存......
  • XGBoost使用
    XGBoost使用Datetime:2023-03-12T20:25+08:00Categories:Python|MachineLearning我从未搭建过任何机器学习模型,所以在Youtube上找到一个XGBoost的简单教程跟......
  • 爬虫相关 requests高级用法、解析json、ssl认证(了解)、使用代理(重要)、超时设置、
    requests高级用法解析json#发送http请求,返回的数据会有xml格式,也有json格式importrequestsdata={'cname':'','pid':'','keyword':'500','page......