Android 彩色Toast的實(shí)現(xiàn)代碼
Android默認(rèn)的Toast太丑了,我們來(lái)封裝一個(gè)花里胡哨的Toast吧,就叫ColoredToast。
Github:https://github.com/imcloudfloating/DesignApp
效果:
Toast有一個(gè)setView方法,通過(guò)它我們可以設(shè)置自定義的布局,這里我只是加入了改變背景色,如果你有其它需求,比如加上圖標(biāo)也是可以的。
布局文件:一個(gè)FrameLayout和顯示消息的TextView
<?xml version="." encoding="utf-"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/toast_message" android:layout_width="wrap_content" android:layout_height="dp" android:paddingStart="dp" android:paddingEnd="dp" android:gravity="center" android:textSize="sp" tools:text="This is a toast message" /> </FrameLayout>
2.Java代碼:
用LayoutInflater來(lái)加載布局,然后用setView將布局設(shè)置為T(mén)oast的根View,通過(guò)自定義方法來(lái)設(shè)置Toast的消息和背景色,這里背景色是給TextView設(shè)置的,假如你想加上圖標(biāo)和其它元素,通過(guò)findViewById來(lái)設(shè)置即可。
這里我用的是GradientDrawable來(lái)作為T(mén)oast的背景,setColor方法背景色,setCornerRadius設(shè)置圓角半徑,最后將他作為T(mén)extView的背景就可以了。如果你不想用它,也可以直接使用xml文件來(lái)作為背景,不過(guò)這樣就不方便靈活設(shè)置顏色了。
package com.cloud.customviews; import android.content.Context; import android.graphics.drawable.GradientDrawable; import android.support.annotation.ColorRes; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.StringRes; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ColoredToast extends Toast { @IntDef(value = { LENGTH_SHORT, LENGTH_LONG }) @interface Duration {} private ColoredToast(Context context) { super(context); } public static class Maker { private Context mContext; private ColoredToast mToast; private View mToastView; private TextView mTextMessage; public Maker(Context context) { mContext = context; mToast = new ColoredToast(context); mToastView = LayoutInflater.from(context).inflate(R.layout.toast_colored, null); mTextMessage = mToastView.findViewById(R.id.toast_message); } /** * Set text color and background color for toast by resource id */ public Maker setColor(@ColorRes int textColor, @ColorRes int backgroundColor) { GradientDrawable drawable = new GradientDrawable(); drawable.setColor(mContext.getColor(backgroundColor)); drawable.setCornerRadius(mTextMessage.getLayoutParams().height / ); mToastView.setBackground(drawable); mTextMessage.setTextColor(mContext.getColor(textColor)); return this; } /** * Set position * @see android.view.Gravity */ public Maker setGravity(int gravity, int xOffset, int yOffset) { mToast.setGravity(gravity, xOffset, yOffset); return this; } public ColoredToast makeToast(@StringRes int resId, @Duration int duration) { mTextMessage.setText(resId); mToast.setView(mToastView); mToast.setDuration(duration); return mToast; } public ColoredToast makeToast(@NonNull String text, @Duration int duration) { mTextMessage.setText(text); mToast.setView(mToastView); mToast.setDuration(duration); return mToast; } } }
相關(guān)文章
Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
沉浸式就是要給用戶(hù)提供完全沉浸的體驗(yàn),使用戶(hù)有一種置身于虛擬世界之中的感覺(jué)。沉浸式模式就是整個(gè)屏幕中顯示都是應(yīng)用的內(nèi)容,沒(méi)有狀態(tài)欄也沒(méi)有導(dǎo)航欄,用戶(hù)不會(huì)被一些系統(tǒng)的界面元素所打擾,讓我們來(lái)實(shí)現(xiàn)下網(wǎng)上傳的沸沸揚(yáng)揚(yáng)的安卓沉浸式狀態(tài)欄2021-11-11Flutter使用Provider進(jìn)行狀態(tài)管理的實(shí)現(xiàn)
Provider是Flutter中一個(gè)非常流行的狀態(tài)管理工具,它可以幫助開(kāi)發(fā)者更有效地管理Widget樹(shù)中的數(shù)據(jù),本文主要介紹了Flutter使用Provider進(jìn)行狀態(tài)管理的實(shí)現(xiàn),感興趣的可以了解一下2024-04-04DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android 實(shí)現(xiàn)長(zhǎng)按彈出PopupMenu 菜單欄
這篇文章主要介紹了Android 實(shí)現(xiàn)長(zhǎng)按彈出PopupMenu 菜單欄,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12分享Android平板電腦上開(kāi)發(fā)應(yīng)用程序不能全屏顯示的問(wèn)題解決
在一個(gè)8寸屏的Android平板電腦上開(kāi)發(fā)應(yīng)用程序(游戲程序),開(kāi)始的時(shí)候,總是不能全屏顯示,也不知道怎么設(shè)置才可以2013-05-05Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)EditText清空按鈕功能相關(guān)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11Android自定義viewGroup實(shí)現(xiàn)點(diǎn)擊動(dòng)畫(huà)效果
這篇文章主要介紹了Android自定義viewGroup實(shí)現(xiàn)點(diǎn)擊動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單水波紋效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08