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)文章
Android RecyclerView詳解之實(shí)現(xiàn) ListView GridView瀑布流效果
RecyclerView 是Android L版本中新添加的一個(gè)用來(lái)取代ListView的SDK,它的靈活性與可替代性比listview更好2016-07-07
Android開(kāi)發(fā)之軟鍵盤(pán)用法實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之軟鍵盤(pán)用法,實(shí)例分析了Android軟鍵盤(pán)的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05
Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
所謂傳感器能夠探測(cè)如光、熱、溫度、重力、方向 等等的功能,本文給大家介紹Android傳感器應(yīng)用之重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球,對(duì)android重力傳感器相關(guān)知識(shí)感興趣的朋友一起看看吧2015-12-12
Android實(shí)現(xiàn)一周時(shí)間早中晚排班表
項(xiàng)目需求需要實(shí)現(xiàn)一個(gè)動(dòng)態(tài)添加,修改一周早中晚時(shí)間排班表,文章給大家提供了實(shí)現(xiàn)代碼,需要的朋友參考下吧2018-07-07
android通過(guò)拼音搜索中文的功能實(shí)現(xiàn)代碼
這篇文章主要介紹了android通過(guò)拼音搜索中文的功能實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Android利用爬蟲(chóng)實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android利用爬蟲(chóng)實(shí)現(xiàn)模擬登錄的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
Android自定義popupwindow實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android視頻處理之動(dòng)態(tài)時(shí)間水印效果
這篇文章主要A為大家詳細(xì)介紹了Android視頻處理之動(dòng)態(tài)時(shí)間水印效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

