这篇博客写的是 个性化Toast,效果图:
实现很简单,还用的是Toast, 只不过是给它setView了。看代码
1. 写toast弹出的布局
toast_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/toast_bg" //下面给出toast_bg.xml这个代码
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerCrop"
android:background="@drawable/toast_ico"/>
<TextView
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="再按一次退出程序"/>
</LinearLayout>
drawable/toast_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="15dp"/>
<solid android:color="#3F51B5"/>
<padding android:bottom="5dp"
android:top="5dp"
android:left="10dp"
android:right="10dp"/>
</shape>
2.toast的布局已经写好了,然后直接创建toast
Toast toast = new Toast(this);
toast.setView(LayoutInflater.from(this).inflate(R.layout.toast_layout,null));
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
于是例图的效果就实现啦...