android自定義toast(widget開(kāi)發(fā))示例
1、Toast控件:
通過(guò)查看源代碼,發(fā)現(xiàn)Toast里面實(shí)現(xiàn)的原理是通過(guò)服務(wù)Context.LAYOUT_INFLATER_SERVICE獲取一個(gè)LayoutInflater布局管理器,從而獲取一個(gè)View對(duì)象(TextView),設(shè)置內(nèi)容將其顯示
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
定義布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_my_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/notification" />
<TextView
android:id="@+id/tv_my_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="text"
/>
</LinearLayout>
自定義MyToast類(lèi):
public class MyToast {
/**
* 顯示自定義的土司
* @param context 上下文
* @param iconid 圖標(biāo)的id
* @param text 顯示的文本
*/
public static void showToast(Context context,int iconid, String text){
View view = View.inflate(context, R.layout.my_toast, null);
TextView tv = (TextView) view.findViewById(R.id.tv_my_toast);
ImageView iv = (ImageView) view.findViewById(R.id.iv_my_toast);
iv.setImageResource(iconid);
tv.setText(text);
Toast toast = new Toast(context);
toast.setDuration(0);
toast.setView(view);
toast.show();
}
}
相關(guān)文章
DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法(附測(cè)試demo)
這篇文章主要介紹了Android實(shí)現(xiàn)EditText控件禁止輸入內(nèi)容的方法,涉及Android針對(duì)EditText控件屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-12-12ubuntu 12.10 上 android 編譯環(huán)境搭建的深入解析
本篇文章是對(duì)ubuntu 12.10上android 編譯環(huán)境的搭建進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果
這篇文章主要介紹了Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果的方法,文章最后還附帶了監(jiān)聽(tīng)程序是否進(jìn)入后臺(tái)的判斷方法,需要的朋友可以參考下2016-02-02完美解決Android客戶端RSA解密部分亂碼的問(wèn)題
下面小編就為大家分享一篇完美解決Android客戶端RSA解密部分亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android Studio實(shí)現(xiàn)幀動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)幀動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android Cocos Creator游戲開(kāi)發(fā)平臺(tái)打包優(yōu)化實(shí)現(xiàn)方案
Cocos Creator是一款輕量、高效、免費(fèi)開(kāi)源的跨平臺(tái)游戲引擎,同時(shí)也是實(shí)時(shí)3D內(nèi)容創(chuàng)作平臺(tái),不僅支持2D、3D的游戲開(kāi)發(fā),同時(shí)在HMI、IoT、XR、虛擬人偶等領(lǐng)域,均可提供一套完善的行業(yè)解決方案2022-11-11