my_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/toast_bg">
<TextView android:layout_width="wrap_content" android:id="@+id/TextViewInfo"
android:gravity="center" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:textColor="#ffffffff"></TextView>
</LinearLayout>
代码调用:
/**
* 显示一个Toast
* @param text:toast上显示的文字
* @param loctionX:toast的X坐标
* @param locationY:toast的Y坐标
*/
void ShowMyToast(String text, int loctionX, int locationY) {
View toastRoot = getLayoutInflater().inflate(R.layout.my_toast, null);
Toast toast = new Toast(getApplicationContext());
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER, loctionX, locationY);
// toast上显示的文字
TextView title = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
title.setText(text);
// 设置显示时间
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
本文介绍了一种在Android中自定义Toast消息提示的方式,包括XML布局文件的设计和Java代码的具体实现过程。通过这种方式,开发者可以灵活地调整Toast的位置及样式。
1133

被折叠的 条评论
为什么被折叠?



