Android开发——Toast的基本使用

一、直接调用Toast类的makeText()方法创建

// @parm1 上下文对象
// @parm2 显示的内容
// @parm3 显示的时间,只有LONG和SHORT两种
Toast.makeText(MainActivity.this,"提示的内容",Toast.LENGTH_LONG).show();

二、常用方法

方法描述
setGravity设置显示位置
findViewById(android.R.id.message)获得显示的文本,然后进行设置颜色,或者大小
toast.getView()活得布局控件,然后设置背景或者添加其它控件

三、自定义Toast控件

1、创建一个shape的资源文件作为自定义Toast的边界框

bg_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置透明背景色 -->
    <solid android:color="#E21C1C1C" />
    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="1px"
        android:color="#FFFFFF" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:radius="25dp" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="15dp"
        android:right="15dp"
        android:top="5dp" />
</shape>

2、创建一个自定义的XML布局作为自定义Toast的布局

view_toast_custom.xml

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

    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="10dp"
        android:scaleType="centerInside"
        android:src="@drawable/icon1" />

    <TextView
        android:id="@+id/tv_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="20sp"
        android:textColor="#fff"/>

</LinearLayout>

3、创建一个自定义Toast类

CustomToast.java

public class CustomToast extends Toast {

    private String m_str;
    private int m_time;
    private Context m_contect;
    private ImageView img_logo;
    private TextView tv_msg;


    public CustomToast(Context context,String str, int time) {
        super(context);
        this.m_contect = context;
        this.m_str = str;
        this.m_time = time;
        setToastView();
   }


    //设置布局文件
    private void setToastView(){
        LayoutInflater inflater = ((Activity)m_contect).getLayoutInflater();
        View view = inflater.inflate(R.layout.view_toast_custom,(ViewGroup)((Activity)m_contect).findViewById(R.id.lly_toast));
        img_logo = (ImageView)view.findViewById(R.id.img_logo);
        tv_msg = (TextView)view.findViewById(R.id.tv_msg);

        setView(view);
        setToastText(m_str);
    }


    //设置显示时间
    public CustomToast setTime(){
        if(m_time>=5)
            setDuration(Toast.LENGTH_LONG);
        else
            setDuration(Toast.LENGTH_SHORT);
        return this;
    }

    //设置Toast图标
    public CustomToast setToastImage(int id){
        img_logo.setImageResource(id);
        return this;
    }

    //设置文本内容
    public CustomToast setToastText(String text){
        m_str = text;
        tv_msg.setText(text);
        return this;
    }
}

4、在MainActivity创建自定义Toast控件

new CustomToast(MainActivity.this,"",20).setToastImage(R.drawable.icon2).setToastText("这是我的头像").show();

5、显示效果、

在这里插入图片描述

参考教程: 菜鸟教程:Toast的基本使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值