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

短代码-带有背景的Toast

原创
作者头像
奶油话梅糖
修改2021-03-16 14:25:06
1K0
修改2021-03-16 14:25:06
举报

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

DiyToast.class - 主要接口

代码语言:txt
复制
/**
 * @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文件)

代码语言:txt
复制
<?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文件)

代码语言:txt
复制
<?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类中使用如下方法:

代码语言:txt
复制
DiyToast.showToast(getApplicationContext(), "请输入用户名");

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

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