Android自定義Toast樣式實(shí)現(xiàn)方法詳解
原生Toast樣式

自定義Toast樣式

創(chuàng)建樣式
所謂自定義一個(gè)Toast就是建立一個(gè)布局文件,然后使用一個(gè)view容器承載,然后顯示出來(lái)。Toast畢竟只是起到一個(gè)提示作用,不必要放太多控件去填充,反而會(huì)顯得內(nèi)容很擁擠,一般一個(gè)標(biāo)題,一張圖片,一段簡(jiǎn)短文字即可。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_radius_10">
<LinearLayout
android:layout_width="120dp"
android:layout_height="100dp"
android:gravity="center"
android:orientation="vertical"
android:layout_gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_error" />
<TextView
android:id="@+id/ErrorTips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="密碼錯(cuò)誤,請(qǐng)重試!"
android:textColor="#099A9F"
android:textSize="15sp"
android:ellipsize="end"/>
</LinearLayout>
</FrameLayout>
封裝
因?yàn)槲覀冊(cè)陂_(kāi)發(fā)中需要頻繁使用Toast,所以使用一個(gè)類將其封裝起來(lái),復(fù)用其功能,增強(qiáng)代碼可讀性,可行性。根據(jù)自己的開(kāi)發(fā)需求,可以向外暴露修改內(nèi)容,圖片,顏色,文字大小等方法,由于Toast內(nèi)附控件較少,就可以直接將其控件ID和布局文件定義為常量引用。
public class ToastFormat {
private Context context;
private TextView tipsText;
private Toast toast = null;
private static final int ContentID = R.id.ErrorTips;
private static final int LayoutID = R.layout.passworderror;
public ToastFormat(Context context){
this.context = context;
}
public void InitToast(){
if (toast == null) {
toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(LayoutID, null, false);
tipsText = view.findViewById(ContentID);
toast.setView(view);
}
}
public void setGravity(int gravity){
toast.setGravity(gravity, 0, 0);
}
public void setText(String tips){
tipsText.setText(tips);
}
public void show(){
toast.show();
}
public void setShowTime(int time){
toast.setDuration(time);
}
public void setTextColor(int color){
tipsText.setTextColor(context.getResources().getColor(color));
}
public void setTextSize(float size){
tipsText.setTextSize(size);
}
}
引用
定義對(duì)象
private ToastFormat format;
初始化
format = new ToastFormat(MainActivity.this); format.InitToast();
設(shè)置顯示文字內(nèi)容
format.setText("自定義Toast");最后顯示 Toast
format.show();
到此這篇關(guān)于Android自定義Toast樣式實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Android自定義Toast內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義控件實(shí)現(xiàn)下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)下拉刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Flutter 日期時(shí)間DatePicker控件及國(guó)際化
這篇文章主要介紹了Flutter 日期時(shí)間DatePicker控件及國(guó)際化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Flutter中嵌入Android 原生TextView實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Flutter中嵌入Android 原生TextView的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android 有道詞典的簡(jiǎn)單實(shí)現(xiàn)方法介紹
本篇文章小編為大家介紹,Android 有道詞典的簡(jiǎn)單實(shí)現(xiàn)方法介紹。需要的朋友參考下2013-04-04
Android屬性動(dòng)畫實(shí)現(xiàn)布局的下拉展開(kāi)效果
這篇文章主要為大家詳細(xì)介紹了Android屬性動(dòng)畫實(shí)現(xiàn)布局的下拉展開(kāi)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局
這篇文章主要為大家詳細(xì)介紹了ViewPager+RadioGroup實(shí)現(xiàn)左右滑動(dòng)卡片布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android和JavaScript相互調(diào)用的方法
這篇文章主要介紹了Android和JavaScript相互調(diào)用的方法,實(shí)例分析了Android的WebView執(zhí)行JavaScript及JavaScript訪問(wèn)Android的技巧,需要的朋友可以參考下2015-12-12
Android編程實(shí)現(xiàn)對(duì)電池狀態(tài)的監(jiān)視功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)對(duì)電池狀態(tài)的監(jiān)視功能,涉及Android基于廣播實(shí)現(xiàn)針對(duì)電源電量的判定與監(jiān)視技巧,需要的朋友可以參考下2016-11-11
Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)
這篇文章主要介紹了Android仿餓了么加入購(gòu)物車旋轉(zhuǎn)控件自帶閃轉(zhuǎn)騰挪動(dòng)畫的按鈕效果(實(shí)例詳解)的相關(guān)資料,需要的朋友可以參考下2017-01-01

