自定義 Snackbar(無需檢視)
建立一個 Snackbar 而不需要將檢視傳遞給 Snackbar,所有佈局都在 android.R.id.content 中的 android 中建立。
public class CustomSnackBar {
public static final int STATE_ERROR = 0;
public static final int STATE_WARNING = 1;
public static final int STATE_SUCCESS = 2;
public static final int VIEW_PARENT = android.R.id.content;
public CustomSnackBar(View view, String message, int actionType) {
super();
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.parseColor("#ffffff"));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
textView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
switch (actionType) {
case STATE_ERROR:
snackbar.getView().setBackgroundColor(Color.parseColor("#F12B2B"));
break;
case STATE_WARNING:
snackbar.getView().setBackgroundColor(Color.parseColor("#000000"));
break;
case STATE_SUCCESS:
snackbar.getView().setBackgroundColor(Color.parseColor("#7ED321"));
break;
}
snackbar.show();
}
}
用於通話類
new CustomSnackBar(findViewById(CustomSnackBar.VIEW_PARENT)
,message
,CustomSnackBar.STATE_ERROR);