toast可以设置自定义的view和显示位置。下面是一个简单的例子,复杂些的就是改变其布局文件就可以了。
/** * @author BMR * @ClassName: ToastWithTwoText * @Description: TODO: * @date 2015/12/22 14:24 */ public class ToastWithTwoText { private static ToastWithTwoText toastWithTwoText; private Toast toast; private Context mContext; private ToastWithTwoText(Context context) { this.mContext = context; } public static ToastWithTwoText createToastConfig(Context context) { if (toastWithTwoText == null) { toastWithTwoText = new ToastWithTwoText(context); } return toastWithTwoText; } /** * 显示Toast * * @param tvStrOne * @param tvStrTwo */ public void ToastShow(String tvStrOne, String tvStrTwo) { View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_toast_with_two_text, null); TextView tvOne = (TextView) layout.findViewById(R.id.tv_text_one); TextView tvTwo = (TextView) layout.findViewById(R.id.tv_text_two); tvOne.setText(tvStrOne); tvTwo.setText(tvStrTwo); toast = new Toast(mContext); toast.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } public void ToastShow(int idStrOne, int idStrTwo) { ToastShow(mContext.getString(idStrOne), mContext.getString(idStrTwo)); } }
布局文件代码
<?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:layout_gravity="center" android:paddingLeft="@dimen/common_16" android:paddingRight="@dimen/common_16" android:background="@drawable/bg_circle_cornor_rect" android:orientation="vertical"> <TextView android:id="@+id/tv_text_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/common_16" android:gravity="center" android:textColor="@color/trans_white2" android:textSize="16dp"/> <TextView android:id="@+id/tv_text_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/common_16" android:gravity="center" android:textColor="@color/trans_white2" android:textSize="16dp"/> </LinearLayout>
圆角背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/trans_black_87" /> <corners android:topLeftRadius="8dp" android:topRightRadius="8dp" android:bottomRightRadius="8dp" android:bottomLeftRadius="8dp"/> </shape>
资源字段
<color name="trans_black_87">#DD000000</color> <!-- 87% trans --> <color name="trans_white2">#ccffffff</color>
<dimen name="common_16">16dp</dimen>
原文:http://www.cnblogs.com/permanent2012moira/p/5066752.html