前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >短代码-带有背景的Toast

短代码-带有背景的Toast

作者头像
奶油话梅糖
发布2025-03-03 14:27:42
发布2025-03-03 14:27:42
5700
代码可运行
举报
运行总次数:0
代码可运行

本文编写于 165 天前,最后修改于 159 天前,其中某些信息可能已经过时。

这里逐渐收集了本人自己编写的「短代码」和「重复发明轮子(Reinventing the wheel)」。所有的短代码可前往 “时光”-“文章标签”-“短代码” 查看

DiyToast.class - 主要接口
代码语言:javascript
代码运行次数:0
复制
/**
 * @author Administrator
 * @year 2019
 * @Todo TODO 自定义Toast
 * @package_name com.example.shengsaidemo2019.toats
 * @project_name 2019ShengSaiDemo
 * @file_name DiyToast.java
 */
public class DiyToast {
    public static Toast toast;// 新建Toast

    public static void showToast(Context context, String string) {
        View toastRoot = LayoutInflater.from(context).inflate(
                R.layout.my_toast, null, false);
        TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
        if (toast == null) {// 如果当前没有toast正在显示
            toast = Toast.makeText(context, string, Toast.LENGTH_SHORT);// 展示Toast
            toast.setView(toastRoot);
            tv.setText(string);
        } else {// 如果有正在显示的toast
            toast.setView(toastRoot);
            tv.setText(string);
        }
        toast.show();// 展示Toast
    }
}
my_toast.xml(layout文件夹下的xml文件)
代码语言:javascript
代码运行次数:0
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shaper_toast_show_back"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="15sp"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/TextViewInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15sp"
                android:layout_marginTop="15sp"
                android:text="text"
                android:textColor="#fff" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
shaper_toast_show_back.xml(drawable文件夹下的xml文件)
代码语言:javascript
代码运行次数:0
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="15sp" />
    <solid android:color="#3399FF" />
    <stroke
        android:width="8sp"
        android:color="#70FFFFFF" />
</shape>
调用方法

在任意Class类中使用如下方法:

代码语言:javascript
代码运行次数:0
复制
DiyToast.showToast(getApplicationContext(), "请输入用户名");
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • DiyToast.class - 主要接口
  • my_toast.xml(layout文件夹下的xml文件)
  • shaper_toast_show_back.xml(drawable文件夹下的xml文件)
  • 调用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档