1、Toast控件:
通過查看源代碼,發(fā)現(xiàn)Toast里面實現(xiàn)的原理是通過服務Context.LAYOUT_INFLATER_SERVICE獲取一個LayoutInflater布局管理器,從而獲取一個View對象(TextView),設(shè)置內(nèi)容將其顯示
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;
}
定義布局文件:
自定義MyToast類:
/**
* 顯示自定義的土司
* @param context 上下文
* @param iconid 圖標的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();
}
}
|
新聞熱點
疑難解答
圖片精選