Android - 自訂Toast Layout

Android - 自訂Toast Layout

LayoutInflater layoutInflater = ctx.getLayoutInflater();
View layout = layoutInflater.inflate(R.layout.layout_toast_custom, (ViewGroup) ctx.findViewById(R.id.lin_toast_custom));
TextView toastText = (TextView) layout.findViewById(R.id.txtv_msg);
toastText.setText(toastMessage);
Toast customToast = new Toast(ctx.getApplicationContext());
customToast.setDuration(Toast.LENGTH_LONG);
customToast.setView(layout);
customToast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
customToast.show();

我自己刻需要的layout ,layout_toast_custom,該lin_toast_custom,是根節點id。

其它可以自己嘗試。

另外整個背景的長度寬度,建議不要放在根節點,背景也不要在根節點,

而是根節點裡面在包一層layout,用該layout處理背景,會比較簡單點。