Android 彩色Toast的實(shí)現(xiàn)代碼
Android默認(rèn)的Toast太丑了,我們來封裝一個(gè)花里胡哨的Toast吧,就叫ColoredToast。
Github:https://github.com/imcloudfloating/DesignApp
效果:

Toast有一個(gè)setView方法,通過它我們可以設(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來加載布局,然后用setView將布局設(shè)置為Toast的根View,通過自定義方法來設(shè)置Toast的消息和背景色,這里背景色是給TextView設(shè)置的,假如你想加上圖標(biāo)和其它元素,通過findViewById來設(shè)置即可。
這里我用的是GradientDrawable來作為Toast的背景,setColor方法背景色,setCornerRadius設(shè)置圓角半徑,最后將他作為TextView的背景就可以了。如果你不想用它,也可以直接使用xml文件來作為背景,不過這樣就不方便靈活設(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 完全退出當(dāng)前應(yīng)用程序的四種方法
Android程序有很多Activity,比如說主窗口A,調(diào)用了子窗口B,如果在B中直接finish(), 接下里顯示的是A。在B中如何關(guān)閉整個(gè)Android應(yīng)用程序呢?本人總結(jié)了幾種比較簡(jiǎn)單的實(shí)現(xiàn)方法2016-02-02
Android中@id和@+id及@android:id的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于Android中@id和@+id及@android:id的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
本篇文章主要介紹了android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程
最近在項(xiàng)目中要用到曲線圖,于是在網(wǎng)上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我還用過基于html5的jsChart來做過,不過最終還是選擇了MPAndroidChart來做本文介紹了Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程,需要的朋友可以參考下。2018-03-03
詳解Flutter中StatefulBuilder組件的使用
StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時(shí)僅重建某些小區(qū)域而無(wú)需付出太多努力。本文將來詳細(xì)講講它的使用,需要的可以參考一下2022-05-05
Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開
這篇文章主要介紹了Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開,感興趣的小伙伴們可以參考一下2015-12-12
Android讀取本地json文件的方法(解決顯示亂碼問題)
這篇文章主要介紹了Android讀取本地json文件的方法,結(jié)合實(shí)例形式對(duì)比分析了解決顯示亂碼問題的方法,需要的朋友可以參考下2016-06-06
flutter 微信聊天輸入框功能實(shí)現(xiàn)
這篇文章主要介紹了flutter 微信聊天輸入框功能實(shí)現(xiàn),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

