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(); }