Android自定义Toast

怎么改变Android默认Toast的颜色和大小,自定义我们自己需要的效果

思路:我在这里封装了一个Toast工具类,这样在想用时,直接调用里面的方法就ok了!

有两个方法:第一个是文本加图片显示的,第二个是纯文本显示的。

上代码。。。


public class ToastUtil {

 

    //显示文本+图片的Toast

    public static void showImageToas(Context context,String message){

        View toastview= LayoutInflater.from(context).inflate(R.layout.toast_image_layout,null);

        TextView text = (TextView) toastview.findViewById(R.id.tv_message);

        text.setText(message);    //要提示的文本

        Toast toast=new Toast(context);   //上下文

        toast.setGravity(Gravity.CENTER,0,0);   //位置居中

        toast.setDuration(Toast.LENGTH_SHORT);  //设置短暂提示

        toast.setView(toastview);   //把定义好的View布局设置到Toast里面

        toast.show();

    }

    //显示文本的Toast

    public static void showTextToas(Context context,String message){

        View toastview= LayoutInflater.from(context).inflate(R.layout.toast_text_layout,null);

        TextView text = (TextView) toastview.findViewById(R.id.tv_message);

        text.setText(message); 

        Toast toast=new Toast(context);

        toast.setGravity(Gravity.CENTER,0,0);

        toast.setDuration(Toast.LENGTH_SHORT);

        toast.setView(toastview);

        toast.show();

    }

}

图片+文本的布局代码 toast_image_layout.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical"

    android:background="@drawable/bg_toast"

    android:gravity="center">

    <ImageView

        android:layout_width="match_parent"

        android:layout_height="35dp"

        android:padding="5dp"

        android:layout_gravity="center"

        android:src="@drawable/icon_ok"/>

    <TextView

        android:id="@+id/tv_message"

        android:layout_width="match_parent"

        android:gravity="center"

        android:layout_height="wrap_content"

        android:textColor="@color/white"

        android:text="Toast"

        android:padding="5dp"

        android:textSize="16sp"

        />

</LinearLayout>

纯文本的布局代码 toast_text_layout.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical"

    android:background="@drawable/bg_toast"

    android:gravity="center">

    <TextView

        android:id="@+id/tv_message"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textColor="@color/white"

        android:text="Toast"

        android:paddingTop="10dp"

        android:paddingLeft="15dp"

        android:paddingRight="15dp"

        android:paddingBottom="15dp"

        android:textSize="16sp"

        />

</LinearLayout>

布局用到的背景需要自定义形状shape,在res文件下的drawable中new一个Drawable resource file,名称为bg_toast,贴上代码


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <solid android:color="@color/orange"/>

    <corners android:bottomLeftRadius="20dp"

        android:bottomRightRadius="20dp"

        android:topLeftRadius="20dp"

        android:topRightRadius="20dp"/>

</shape>

走到这里就是万事具备,直接调用相应的方法就可以了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值