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()
        }
    }

直接调用方法就行!

相关推荐
Lary_Rock1 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
王江奎1 小时前
Android FFmpeg 交叉编译全指南:NDK编译 + CMake 集成
android·ffmpeg
limingade2 小时前
手机打电话通话时如何向对方播放录制的IVR引导词声音
android·智能手机·蓝牙电话·手机提取通话声音
hepherd3 小时前
Flutter 环境搭建 (Android)
android·flutter·visual studio code
_一条咸鱼_3 小时前
揭秘 Android ListView:从源码深度剖析其使用原理
android·面试·android jetpack
_一条咸鱼_3 小时前
深入剖析 Android NestedScrollView 使用原理
android·面试·android jetpack
_一条咸鱼_3 小时前
揭秘 Android ScrollView:深入剖析其使用原理与源码奥秘
android·面试·android jetpack
_一条咸鱼_3 小时前
深入剖析 Android View:从源码探寻使用原理
android·面试·android jetpack
_一条咸鱼_3 小时前
揭秘 Android View 绘制原理:从源码剖析到极致理解
android·面试·android jetpack