2

我知道我可以使用自定义 toast 布局轻松完成所有这些操作,但是在创建自定义布局时,我将不再使用系统的默认 toast 视图。

示例:在 Android 2.2 和 Android 4.0 中,Toast 看起来当然不同。如果我为我的 toast 创建一个自定义视图,那么它在两个版本中看起来完全一样,但我想要的是它保留它的......“Androidiness”,因为没有更好的词。基本上,有人知道默认的 toast XML 布局吗?这甚至可能吗?

4

3 回答 3

6

这是 . 使用的默认布局Toasts

瞬态通知

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/toast_frame">

<TextView
    android:id="@android:id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textAppearance="@style/TextAppearance.Small"
    android:textColor="@color/bright_foreground_dark"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"/>
</LinearLayout>

这些是进入和退出动画

进入

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@interpolator/decelerate_quad"
        android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_longAnimTime" />

出口

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@interpolator/accelerate_quad" 
        android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_longAnimTime" />

toast_frame_holo.9是用于背景的资源的名称。搜索 SDK 以查找所有尺寸。

这是完整的来源Toasts

如果您不确定其中任何一个,请在 SDK 中搜索所有资源,这就是我查看的地方。

于 2012-05-03T00:54:17.830 回答
0

在我的 ICS 设备上,由于连接丢失,我最近收到一条消息发送失败。它看起来像吐司,但字体大小不同,有两行长,有红色边框。它基本上说消息发送失败(或连接错误)。

这可能与您正在寻找的相似?

于 2012-05-03T04:33:55.547 回答
0

我也想保留默认的 toast,但找到了解决方案,该解决方案向我展示了如何为所有系统创建单个 toast 布局。所以我创建了这个助手。我知道,这是一个 hack,但它对我来说很好。

在这里,我在文本前面的 toast 中添加了一个图像,而没有触及其原始外观。您可以在此处轻松修改文本颜色和大小。

也许有人可能对此代码片段感兴趣......

private static Toast makeImageToast(int imageResId, CharSequence text, int duration) {
    Toast toast = Toast.makeText(mContext, text, duration);

    View rootView = toast.getView();
    LinearLayout linearLayout = null;
    TextView messageTextView = null;

    // check (expected) toast layout
    if (rootView instanceof LinearLayout) {
        linearLayout = (LinearLayout) rootView;

        if (linearLayout.getChildCount() == 1) {
            View child = linearLayout.getChildAt(0);

            if (child instanceof TextView) {
                messageTextView = (TextView) child;
            }
        }
    }

    // cancel modification because toast layout is not what we expected
    if (linearLayout == null || messageTextView == null) {
        // failed to create image toast layout, using usual toast
        return toast;
    }

    ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
    ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL;

    // convert dip dimension
    int imageSize = dipToPixel(25);
    int imageMargin = dipToPixel(15);

    // setup image view layout parameters
    LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
    imageParams.setMargins(0, 0, imageMargin, 0);
    imageParams.gravity = Gravity.CENTER_VERTICAL;

    // setup image view
    ImageView imageView = new ImageView(mContext);
    imageView.setId(TOAST_IMAGE_ID);
    imageView.setImageResource(imageResId);
    imageView.setLayoutParams(imageParams);

    // modify root layout
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.addView(imageView, 0);

    return toast;
}

public static int dipToPixel(float dip) {
    return (int) (dip * mContext.getResources().getDisplayMetrics().density + 0.5f);
}
于 2012-05-09T21:59:36.080 回答