自定义toast

本文介绍了一种自定义Toast弹窗的方法,包括位置居中显示、包含图片和文字及自定义背景等功能。通过示例代码展示了如何创建并使用自定义Toast布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.app.Activity;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * creator gonghaohao
 * class name:ToastUtil
 * created at 2017/10/16 16:32
 * function describe:自定义toast弹框
 * 位置居中,包含图片、文字,自定义背景
 * modify
 */

public class ToastUtil {
    public static void centerToast(final Activity context, String string) {
        RelativeLayout layout = (RelativeLayout) context.getLayoutInflater().inflate(R.layout.layout_custom_toast, null);
        (layout.findViewById(R.id.tvCheckoutWay)).setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        ((TextView) layout.findViewById(R.id.tvPercent)).setText(string);

        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);//居中
        toast.setView(layout);//setting the view of custom toast layout
        toast.show();
    }
public static void centerPicToast(final Activity context, String string,int pic) {
        RelativeLayout layout = (RelativeLayout) context.getLayoutInflater().inflate(R.layout.layout_custom_toast, null);
        (layout.findViewById(R.id.tvCheckoutWay)).setBackgroundDrawable(context.getResources().getDrawable(pic));
        ((TextView) layout.findViewById(R.id.tvPercent)).setText(string);

        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(layout);//setting the view of custom toast layout
        toast.show();
    }

}

布局文件:layout_custom_toast
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0.5"
        android:background="@drawable/toast_bg">


        <ImageView
            android:id="@+id/tvCheckoutWay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="@mipmap/ic_launcher"></ImageView>

        <TextView
            android:id="@+id/tvPercent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvCheckoutWay"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="输入有误"
            android:textColor="#aabbcc"
            android:textSize="20sp" />
    </RelativeLayout>
</RelativeLayout>
toast背景资源文件:toast_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="8dp"></corners>
    <solid android:color="#000000"></solid>
</shape>
使用:
ToastUtil.centerToast(MainActivity.this, "我是自定义toast");
ToastUtil.centerPicToast(MainActivity.this, "我是自定义toast",R.mipmap.ic_launcher);

效果图
















                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值