You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义Toast报错:LayoutInflater需初始化,求带圆角自定义颜色Toast方案

解决Toast初始化错误并实现圆角自定义颜色Toast

一、修复LayoutInflater未初始化的错误

你遇到的inflater must be initialized错误,是因为仅声明了LayoutInflater变量但未给它赋值。正确的做法是通过上下文获取LayoutInflater实例,修正后的代码如下:

private fun testToast() {
    // 正确初始化LayoutInflater
    val inflater = LayoutInflater.from(context)
    // inflate布局时传入null作为父容器(Toast会自行处理布局挂载)
    val view = inflater.inflate(R.layout.custom_toast_message, null)
    val toast = Toast(context)
    toast.setGravity(Gravity.TOP, 0, 70)
    toast.duration = Toast.LENGTH_LONG
    toast.view = view
    toast.show()
}

二、实现圆角自定义颜色Toast

要去掉方形Toast,不能直接用纯色作为背景,需要给布局根容器设置圆角背景drawable:

1. 创建圆角背景drawable

在res/drawable/目录下新建toast_background.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置背景色 -->
    <solid android:color="@color/background_blue"/>
    <!-- 设置圆角半径,可根据需求调整数值 -->
    <corners android:radius="16dp"/>
    <!-- 可选:添加内边距,避免内容紧贴边缘 -->
    <padding android:left="16dp" android:top="12dp" android:right="16dp" android:bottom="12dp"/>
</shape>

2. 修改自定义Toast布局

将原布局的android:background="@color/background_blue"替换为刚创建的drawable,同时可移除重复的padding配置:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_toasticon"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_gravity="center_vertical"/>

    <TextView
        android:id="@+id/text_view_toast"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.8"
        android:textColor="@color/gauge_red"
        android:fontFamily="@font/roboto"
        android:text="Prueba prueba PRUEBA"/>

</LinearLayout>

3. 兼容性优化(可选,适配Android 11+)

Android 11及以上版本推荐结合Toast.makeText()实现,避免直接创建Toast实例可能的兼容性问题:

private fun showCustomToast() {
    val inflater = LayoutInflater.from(context)
    val view = inflater.inflate(R.layout.custom_toast_message, null)
    // 支持动态修改Toast文本
    val toastText = view.findViewById<TextView>(R.id.text_view_toast)
    toastText.text = "自定义Toast内容"
    
    val toast = Toast.makeText(context, "", Toast.LENGTH_LONG)
    toast.setGravity(Gravity.TOP, 0, 70)
    toast.view = view
    toast.show()
}

内容的提问来源于stack exchange,提问作者Hugo Urbina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 09:05:38