更換 Android 原生 Toast 的樣式

更換 Android 原生 Toast 的樣式

Toast 每個用Android 手機的用戶再熟悉不過,而且各種自定義樣式的 文章也層出不窮
本篇文章并不是要走他們的老路,本文只針對 Toast 的UI樣式,剖析完后再來定制.
本文源碼:https://github.com/didikee/CommonDependence
覺得不錯可以先關注下,后面會更新更多的組件 ?乛?乛?

Toast 使用的 Layout

在源碼中寫到:

View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);

也就是:
com.android.internal.R.layout.transient_notification

其布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/bright_foreground_dark"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        />

</LinearLayout>

文件路徑在:...\SDK\platforms\android-23\data\res\layout\transient_notification.xml

**需要一個個解讀的如下: **

1. Toast 的背景 : android:background="?android:attr/toastFrameBackground"
2. Toast 文字樣式 : android:textAppearance="@style/TextAppearance.Toast"
3. Toast 文字顏色 : android:textColor="@color/bright_foreground_dark"

Toast 的背景 Background

android:background="?android:attr/toastFrameBackground

...\SDK\platforms\android-23\data\res\values\themes.xml中,可以看到,背景其實是一個 Drawable資源

        <!-- Toast attributes -->
        <item name="toastFrameBackground">@drawable/toast_frame</item>

最終在:...\SDK\platforms\android-23\data\res\drawable-xxhdpi\toast_frame.9.png找到了

toast_frame.9.png

本來是想改變.9圖的顏色的,但是Google了很久都沒有解決方案,可能 SVG矢量圖是為了彌補這一缺陷而運用在Android上的.

如果誰有解決方案,麻煩@下我,我也想知道 ?乛?乛?
好了,換一個思路.

經過 馬克曼的測量,再配合 Material Design的設計規(guī)范,得到了最終滿意的尺寸.

MD 習慣的尺寸為 1,2,4,8,16,24,36,48,56......絕大數(shù)情況是偶數(shù)[單位都是dp]

最終布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/shape_toast"
              android:orientation="vertical">
    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="12dp"
        android:paddingTop="12dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:textSize="16sp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        android:textColor="@color/WHITE"
        />

</LinearLayout>

Toast 文字樣式

android:textAppearance="@style/TextAppearance.Toast
文件路徑: ...\SDK\platforms\android-23\data\res\values\style.xml

    <style name="TextAppearance.Toast">
        <item name="fontFamily">sans-serif-condensed</item>
    </style>

設置 `` 有兩種方式:

1. textView.setTextAppearance(R.style.AndroidToast);//api16
2. Typeface typeface = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
            textView.setTypeface(typeface);//api1

第一種看起來方便簡潔,但是要求 api>=16,而 4.0 對應的是 api15,所以還是選擇第二種吧

Toast 文字顏色

android:textColor="@color/bright_foreground_dark"
<color name="bright_foreground_dark">@android:color/background_light</color>
<color name="background_light">#ffffffff</color>

所以默認的顏色是純白的 #FFFFFF

最后貼下 java 代碼

    /**
     * {@link layout/transient_notification.xml}
     * @param content content to show
     * @param longTime short or long
     * @param context context
     * @param textColor toast text color
     * @param toastBackgroundColor toast background color
     */
    public static void showToast(@NonNull Context context, String content, boolean longTime, @ColorInt
            int textColor,@ColorInt int toastBackgroundColor) {
        int type = longTime ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, content, type);
        ViewGroup toastView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout
                .layout_toast, null, false);
        if (toastBackgroundColor != 0) {
            toastView.setBackgroundDrawable(getToastBackground(context, toastBackgroundColor));
        }
        TextView textView = (TextView) toastView.findViewById(android.R.id.message);
        // 內部已經作非空判斷了
        if (textColor!=0){
            textView.setTextColor(textColor);
        }
        Typeface typeface = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
        textView.setTypeface(typeface);
        toast.setView(toastView);
        toast.setText(content);
        toast.show();
    }

    private static Drawable getToastBackground(@NonNull Context context, @ColorInt int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setCornerRadius(DisplayUtil.dp2px(context, 24));
        gradientDrawable.setColor(color);
        return gradientDrawable;
    }

結束

本文源碼:https://github.com/didikee/CommonDependence
覺得不錯可以先關注下,后面會更新更多的組件 ?乛?乛?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末颓鲜,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌叨粘,老刑警劉巖阳液,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡动雹,警方通過查閱死者的電腦和手機焰薄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門拿诸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人塞茅,你說我怎么就攤上這事亩码。” “怎么了野瘦?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵描沟,是天一觀的道長。 經常有香客問我鞭光,道長吏廉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任惰许,我火速辦了婚禮席覆,結果婚禮上,老公的妹妹穿的比我還像新娘汹买。我一直安慰自己佩伤,他們只是感情好聊倔,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著生巡,像睡著了一般耙蔑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上孤荣,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天甸陌,我揣著相機與錄音,去河邊找鬼垃环。 笑死邀层,一個胖子當著我的面吹牛,可吹牛的內容都是我干的遂庄。 我是一名探鬼主播寥院,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼涛目!你這毒婦竟也來了秸谢?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤霹肝,失蹤者是張志新(化名)和其女友劉穎估蹄,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沫换,經...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡臭蚁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了讯赏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片垮兑。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漱挎,靈堂內的尸體忽然破棺而出系枪,到底是詐尸還是另有隱情,我是刑警寧澤磕谅,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布私爷,位于F島的核電站,受9級特大地震影響膊夹,放射性物質發(fā)生泄漏衬浑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一放刨、第九天 我趴在偏房一處隱蔽的房頂上張望嚎卫。 院中可真熱鬧,春花似錦、人聲如沸拓诸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奠支。三九已至,卻和暖如春抚芦,著一層夾襖步出監(jiān)牢的瞬間倍谜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工叉抡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留尔崔,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓褥民,卻偏偏與公主長得像季春,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子消返,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內容