首页 > 其他分享 >Android Toast详解

Android Toast详解

时间:2022-11-01 13:22:52浏览次数:75  
标签:Toast toast findViewById 自定义 详解 Android id view

一般的Toast我就不说了,我这里主要讲一下,自定义toast包括自定义内容和显示的位置

现在Toast有了新情况,在安卓11,现在大家都用SnackBar

 

 

 效果图

 

Toast代码

private void midToast(String str, int showTime, Context mContext)
    {
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.view_toast_custom, (ViewGroup) findViewById(R.id.viewGroup));
        //ImageView img_logo = (ImageView) view.findViewById(R.id.imageView);
        TextView tv_msg = (TextView) view.findViewById(R.id.textView);
        tv_msg.setText(str);
        Toast toast = new Toast(mContext);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(showTime);
        toast.setView(view);
        toast.show();
    }

bg_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置透明背景色 -->
    <solid android:color="@color/cardview_dark_background" />
    <!-- 设置一个黑色边框 -->
    <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:orientation="vertical"
    android:id="@+id/viewGroup"
    android:background="@drawable/bg_toast"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#D8D8D8" />
</LinearLayout>

自定义布局android:src="@drawable/ic_launcher_foreground" />千万记得是默认不是

直接使用

 

midToast("hello",Toast.LENGTH_LONG,this);

 

GitHub地址 下载前给star

 

标签:Toast,toast,findViewById,自定义,详解,Android,id,view
From: https://www.cnblogs.com/Frank-dev-blog/p/16847341.html

相关文章

  • find命令mtime,ctime,atime详解
    1、https://blog.csdn.net/Arlingtonroad/article/details/934664562、1.查找时间说明find./-name“*data*”-mtime+1当前目录下文件名包含data,而且修改时间在4......
  • 软件工程导论课程笔记与详解②
    第一章软件工程概述②(下)目录:1、软件工程介绍2、软件生命周期3、软件过程  1、软件工程的介绍①软件工程的来源:1968年,北约组织NATO召开计算机科学会议。......
  • 注解用法详解——@SuppressWarnings
    作为一名有强迫症的程序员最见不得的事情之一就是程序里有警告出现,还有一大困扰就是在eclipseIDE中,起码前面有警告时会无法加入断点。一般来讲大多数警告是代码不规范或安......
  • 【转】Android之Spinner下拉列表 使用详解
    原文网址:(56条消息)Android之Spinner使用详解_浪漫主义码农的博客-CSDN博客_androidspinnerSpinner(列表选择框)的基本使用当我们的app需要用户输入数据时,除了让用户自......
  • git checkout命令详解 git checkout feature/test123
    gitcheckout命令详解gitcheckoutfeature/test123//切换到feature/test123分支保证当前分支没有修改提交,否则回提示error:Yourlocalchangestothefollowing......
  • Kafka 架构和原理机制 (图文全面详解)
    目录一:Kafka简介二:Kafka基本架构三:Kafka基本原理四:Zookeeper在kafka的作用五:Kafka的特性六:Kafka的应用场景一:Kafka简介ApacheKafka是分布式发布......
  • nvidia-smi命令详解
    nvidia-smi命令用来查看GPU利用率和显存占用情况。在命令行直接输入nvidia-smi命令,得到如下界面:  界面介绍如下所示:    具体参考自如下博客:https://blog......
  • HashMap详解
    HashMap详解HashMap相关介绍HashMap是Java中的比较常见的集合,主要存放的是键值对,以key-value的形式存储,不是线程安全的。它里面的存储的key和value可以为null值,但是key......
  • matlab最小二乘法数据拟合函数详解
    定义:最小二乘法(又称最小平方法)是一种数学优化技术。它通过最小化误差的平方和寻找数据的最佳函数匹配。利用最小二乘法可以简便地求得未知的数据,并使得这些求得的数据与......
  • [Pyhton] SimPy 离散事件模拟框架详解 —— 以一个简单的汽车充电排队模拟为例
    目录一、背景知识二、SimPy讲解2.1SimPy概述2.2基本概念2.3一个汽车开开停停的例子2.4在走走停停过程中增加充电过程(过程交互)2.5共享资源三、后续参考链接附录二......