系統(tǒng)自帶Toast效果分析:系統(tǒng)自帶的彈窗效果,一個(gè)空白框和文字懂讯。
系統(tǒng)Toast的局限性:當(dāng)頻繁點(diǎn)擊按鈕調(diào)用系統(tǒng)彈窗時(shí)荷憋,由于上一個(gè)彈窗還沒(méi)有結(jié)束,便點(diǎn)擊了下一次彈窗褐望,此時(shí)存在一個(gè)彈窗次數(shù)的隊(duì)列勒庄,所以會(huì)看到彈窗長(zhǎng)時(shí)間不消失的效果,所以能夠自定義彈窗的顯示時(shí)間控制便于解決這一bug瘫里,我們正常的需求應(yīng)該是:?jiǎn)未吸c(diǎn)擊实蔽,能夠顯示自定的時(shí)長(zhǎng);頻繁點(diǎn)擊谨读,能夠?qū)崿F(xiàn)下一次點(diǎn)擊時(shí)局装,不管上一次的點(diǎn)擊顯示時(shí)長(zhǎng)有沒(méi)有達(dá)到,都應(yīng)該消失。
系統(tǒng)彈窗效果:
系統(tǒng)彈窗效果實(shí)現(xiàn)code:
Toast.makeText(context,"彈窗文字",Toast.LENGTH_LONG).show();
自定義彈窗效果:
自定義彈窗效果實(shí)現(xiàn)(包括功能:彈窗自定義圖片铐尚、文字拨脉、顯示位置、多長(zhǎng)時(shí)間后顯示彈窗)code:
private void custTost(Context context, String tost_content, int tost_wait_time, final int tost_long_time) {
//tost_wait_time 多長(zhǎng)時(shí)間后顯示彈窗
//tost_content顯示文字內(nèi)容
//tost_long_time顯示彈窗時(shí)長(zhǎng)塑径,尚未實(shí)現(xiàn)該功能
LayoutInflater inflater = getLayoutInflater();
? ? ? ? View layout = inflater.inflate(R.layout.toast_custom_styles,
? ? ? ? ? ? ? ? (ViewGroup) findViewById(R.id.ll_Toast));
? ? ? ? ImageView image = layout.findViewById(R.id.iv_ImageToast);
? ? ? ? image.setImageResource(R.mipmap.ic_launcher);
? ? ? ? TextView text = layout.findViewById(R.id.tv_TextToast);
? ? ? ? text.setText(tost_content);
? ? ? ? final Toast toast =new Toast(context);
? ? ? ? toast.setGravity(Gravity.RIGHT | Gravity.BOTTOM, 20, 50);
? ? ? ? toast.setDuration(Toast.LENGTH_LONG);
? ? ? ? toast.setView(layout);
? ? ? ? final Timer timer =new Timer();
? ? ? ? timer.schedule(new TimerTask() {
@Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ?toast.show();
? ? ? ? ? ? }
}, tost_wait_time);//10表示點(diǎn)擊按鈕之后女坑,Toast延遲10ms后顯示
? ? }
xml布局:toast_custom_styles.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:id="@+id/ll_Toast"
? ? android:layout_width="wrap_content"
? ? android:layout_height="wrap_content"
? ? android:background="#ffffffff"
? ? android:orientation="vertical">
? ? ? ? android:id="@+id/ll_ToastContent"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_marginLeft="1dp"
? ? ? ? android:layout_marginRight="1dp"
? ? ? ? android:layout_marginBottom="1dp"
? ? ? ? android:background="#44000000"
? ? ? ? android:orientation="vertical"
? ? ? ? android:padding="15dip">
? ? ? ? ? ? android:id="@+id/iv_ImageToast"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_gravity="center" />
? ? ? ? ? ? android:id="@+id/tv_TextToast"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:paddingLeft="10dp"
? ? ? ? ? ? android:paddingRight="10dp"
? ? ? ? ? ? android:textColor="#ff000000" />