下面列出了android.widget.Toast#setGravity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void onUploadSuccess(Uri procedure) {
Log.i(TAG, "onUploadSuccess for " + procedure);
String procedureTitle = getProcedureTitle(procedure);
String patientId = ""; // TODO
String msg = "Successfully sent " + procedureTitle + " for patient "
+ patientId + "\n";
//String msg = "Successfully sent " + procedureTitle + " procedure\nwith ID = " + savedProcedureId;
//String msg = "Successfully sent procedure\nwith ID = " + savedProcedureId;
int sizeOfQueue = queue.size();
if (sizeOfQueue != 0) {
msg += "\nThere are still " + sizeOfQueue + "\ncases to be uploaded.";
} else {
msg += "\nAll cases are done uploading.";
}
Toast toast = Toast.makeText(getApplicationContext(), msg,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public boolean onLongClick(View v) {
if (mMenuItem == null || TextUtils.isEmpty(mMenuItem.getTitle())) {
return true;
}
final int[] screenPos = new int[2];
final Rect displayFrame = new Rect();
getLocationOnScreen(screenPos);
getWindowVisibleDisplayFrame(displayFrame);
final Context context = getContext();
final int width = getWidth();
final int height = getHeight();
final int midy = screenPos[1] + height / 2;
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
Toast cheatSheet = Toast.makeText(context, mMenuItem.getTitle(), Toast.LENGTH_SHORT);
if (midy < displayFrame.height()) {
cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT, screenWidth - screenPos[0] - width / 2, height);
} else {
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
}
cheatSheet.show();
return true;
}
public static @CheckResult
Toast snackBar(Context context, CharSequence message_to_show, boolean duration) {
@SuppressLint("InflateParams")
View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_snackbar, null);
AppCompatTextView message = view.findViewById(R.id.message);
message.setText(message_to_show);
Toast toast = new Toast(context);
toast.setView(view);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
if (duration) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
return toast;
}
public void showDescription(int res) {
if (res == 0) return;
TextView view = new TextView(getActivity());
view.setText(res);
view.setTextColor(Color.WHITE);
view.setTextSize(33);
view.setPadding(20, 20, 20, 20);
view.setBackgroundResource(R.color.blink_transparent);
Toast toast = new Toast(getActivity());
toast.setGravity(Gravity.CENTER, 0, 100);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
public static void longShow(int resId){
String content=context.getResources().getString(resId);
if (context==null){
throw new IllegalStateException("TextToast was not initialized");
}
Toast toast= Toast.makeText(context, content, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
* 提示按钮按下时的处理
*
* @param v
*/
public void prompt(View v) {
if (LevelCfg.globalCfg.getPromptNum() > 0) {
game.prompt();
} else {
// 没有可用道具时给出提示
Toast toast = ToastUtil.getToast(this, R.string.tool_no_prompt);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 160);
toast.show();
}
}
@Override
public void showStockDoesNotExist() {
Toast toast = Toast.makeText(this, getResources().getString(R.string.stock_does_not_exist),
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, Gravity.CENTER, 0);
toast.show();
}
private void handleLongClick() {
String contentDesc = getContentDescription().toString();
if (!TextUtils.isEmpty(contentDesc)) {
int[] pos = new int[2];
getLocationInWindow(pos);
Toast t = Toast.makeText(getContext(), contentDesc, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP | Gravity.START, pos[0] - ((contentDesc.length() / 2) * 12), pos[1] - 256);
t.show();
}
}
private void handleLongClick() {
String contentDesc = getContentDescription().toString();
if (!TextUtils.isEmpty(contentDesc)) {
int[] pos = new int[2];
getLocationInWindow(pos);
Toast t = Toast.makeText(mContext, contentDesc, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP | Gravity.LEFT, pos[0] - ((contentDesc.length() / 2) * 12), pos[1] - 128);
t.show();
}
}
@Override
public boolean onLongClick(View v) {
final int[] screenPos = new int[2];
getLocationOnScreen(screenPos);
final Context context = getContext();
final int width = getWidth();
final int height = getHeight();
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
Toast cheatSheet = Toast.makeText(context, mTab.getContentDescription(), Toast.LENGTH_SHORT);
// display below the tab
cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, (screenPos[0] + width / 2) - screenWidth / 2, height);
cheatSheet.show();
return true;
}
private void showOverlay() {
// Generate a toast view with a special layout that will position itself right
// on top of this view's interesting widgets. Sneaky huh?
SecureViewOverlay overlay = (SecureViewOverlay)
getLayoutInflater().inflate(R.layout.secure_view_overlay, null);
overlay.setActivityToSpoof(this);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL, 0, 0);
toast.setView(overlay);
toast.show();
}
/**
* 重排按钮按下时的处理
*
* @param v
*/
public void refresh(View v) {
if (LevelCfg.globalCfg.getRefreshNum() > 0) {
game.refresh();
} else {
// 没有可用道具时给出提示
Toast toast = ToastUtil.getToast(this, R.string.tool_no_refresh);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 160);
toast.show();
}
}
public static void showToast(@NonNull Context context, @NonNull String msg, int duration, int icon) {
if (!msg.isEmpty()) {
CustomToastBinding customToastBinding = CustomToastBinding.inflate(LayoutInflater.from(context));
customToastBinding.setMessage(Html.fromHtml(msg).toString());
Toast toast = new Toast(context);
toast.setGravity(Gravity.BOTTOM, 0, 150);
toast.setView(customToastBinding.getRoot());
toast.setDuration(duration);
toast.show();
}
}
@Override
public boolean onLongClick(View v) {
if (hasText()) {
// Don't show the cheat sheet for items that already show text.
return false;
}
final int[] screenPos = new int[2];
final Rect displayFrame = new Rect();
getLocationOnScreen(screenPos);
getWindowVisibleDisplayFrame(displayFrame);
final Context context = getContext();
final int width = getWidth();
final int height = getHeight();
final int midy = screenPos[1] + height / 2;
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
Toast cheatSheet = IcsToast.makeText(context, mItemData.getTitle(), IcsToast.LENGTH_SHORT);
if (midy < displayFrame.height()) {
// Show along the top; follow action buttons
cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
screenWidth - screenPos[0] - width / 2, height);
} else {
// Show along the bottom center
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
}
cheatSheet.show();
return true;
}
/**
* **********
* Player
* ***********
*/
private void createPlayer(String media) {
releasePlayer();
setupControls();
try {
if (media.length() > 0) {
Toast toast = Toast.makeText(this, media, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
0);
toast.show();
}
// Create a new media player
libvlc = LibVLC.getInstance();
libvlc.setHardwareAcceleration(LibVLC.HW_ACCELERATION_FULL);
libvlc.eventVideoPlayerActivityCreated(true);
libvlc.setSubtitlesEncoding("");
libvlc.setAout(LibVLC.AOUT_OPENSLES);
libvlc.setTimeStretching(true);
libvlc.setChroma("RV32");
libvlc.setVerboseMode(true);
LibVLC.restart(this);
EventHandler.getInstance().addHandler(mHandler);
holder.setFormat(PixelFormat.RGBX_8888);
holder.setKeepScreenOn(true);
MediaList list = libvlc.getMediaList();
list.clear();
list.add(new Media(libvlc, LibVLC.PathToURI(media)), false);
libvlc.playIndex(0);
} catch (Exception e) {
Toast.makeText(this, "Could not create Vlc Player", Toast.LENGTH_LONG).show();
}
}
public static void show(Context context, CharSequence text, int duration) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View view = inflater.inflate(R.layout.tt_speeker_layout, null);
TextView title = (TextView) view.findViewById(R.id.top_tip);
title.setText(text);
Toast toast = new Toast(context.getApplicationContext());
toast.setGravity(
Gravity.FILL_HORIZONTAL | Gravity.TOP,
0,
(int) context.getResources().getDimension(
R.dimen.top_bar_default_height));
toast.setDuration(duration);
toast.setView(view);
toast.show();
}
/**
* Shows a toast with information why the swap failed.
*
* @param text the messages to be displayed.
*/
private Toast showInfoToast(CharSequence text) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
final Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER, 0, 400);
toast.show();
return toast;
}
public static void showCenterToast(Context context, String msg) {
Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
void showToastMessage(final String messageToShow) {
Toast toast = Toast.makeText(fragmentActivity, messageToShow, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
* 根据字符串获取toast
*
* @param ctx
* 上下文
* @param msg
* 字符串
* @return Toast对象
*/
public static Toast getToast(Context ctx, String msg) {
Toast mToast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
mToast.setText(msg);
mToast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 20);
mToast.setDuration(Toast.LENGTH_SHORT);
return mToast;
}