现在的位置: 首页 > 综合 > 正文

android Toast

2013年04月21日 ⁄ 综合 ⁄ 共 956字 ⁄ 字号 评论关闭

 

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

 

抱歉!评论已关闭.