Android 自定义Toast显示View

1、创建一个tosat显示的布局文件:toast_custom.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<com.hjq.shape.layout.ShapeLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingVertical="@dimen/dp_20"
    android:paddingHorizontal="@dimen/dp_30"
    app:shape_radius="@dimen/dp_10"
    app:shape_solidColor="#B3000000">


    <ImageView
        android:id="@+id/toast_icon"
        android:layout_width="@dimen/dp_56"
        android:layout_height="@dimen/dp_56"
        android:src="@mipmap/toast_icon" />


    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="保存成功"
        android:textColor="@color/white"
        android:textSize="16sp" />
</com.hjq.shape.layout.ShapeLinearLayout>

2.通过代码显示出来:

Kotlin 复制代码
  private fun showToast(text : String = "保存成功") {
        Toast(applicationContext).apply {
            duration = Toast.LENGTH_SHORT
            view = layoutInflater.inflate(R.layout.toast_custom, null)
            val tv = view?.findViewById<TextView>(R.id.toast_text)
            tv?.text = text
            show()
        }
    }

直接调用方法就行!

相关推荐
Jason__Young2 小时前
Android ViewModel为什么能够跨越Activity的生命周期?
android
TechMix2 小时前
【性能优化】RenderThread各工作阶段梳理
android·性能优化
草莓熊Lotso2 小时前
MySQL 内置函数指南:日期、字符串、数学函数实战
android·java·linux·运维·数据库·c++·mysql
2401_895521342 小时前
mysql中general_log日志详解
android·数据库·mysql
zh_xuan3 小时前
Android compose 自定义主题
android·compose
一只程序熊3 小时前
uniapp 高德地图 打开选择地址报错,也没有展示出附近的位置
android·uni-app
贤泽3 小时前
Android View 触摸事件分发机制
android·aosp
zh_xuan3 小时前
Android compose 使用viewModel
android·compose
0pen13 小时前
我用 AI 写了一个 Android 群控工具,从零到可用只花了一个下午
android·人工智能
雾江流3 小时前
LSPosed 2.0.0 | 强大的安卓Root框架,支持XP模块
android·软件工程