Android学习笔记(16)---定义自己的Toast

来源:互联网 发布:北美海关数据查询 编辑:程序博客网 时间:2024/04/28 22:12

1、直接调用自带的Toast

Toast.makeText(context, "你设置的闹钟时间到了", Toast.LENGTH_LONG).show();

但是有时候不能满足我们想要的风格,于是...

2、下面来自定义属于自己风格的Toast

toast.xml

<?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"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:shadowColor="#ff000000"        android:shadowRadius="2.75"        android:text="雷人科技有限公司"        android:textSize="45dp" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:shadowColor="#ff000000"        android:shadowRadius="2.75"        android:textSize="45dp" /></LinearLayout>

在代码中需要使用的地方调用:

View view = getLayoutInflater().inflate(R.layout.toast, null);Toast toastStart = new Toast(this);//public void setGravity (int gravity, int xOffset, int yOffset) toastStart.setGravity(Gravity.BOTTOM, 0, 10);toastStart.setDuration(Toast.LENGTH_LONG);toastStart.setView(view);toastStart.show();

3、效果图


4、基于上面的例子,可以定义不同的Toast



原创粉丝点击