当为Toast使用setDuration()时,是否可以设置一个自定义长度或至少比Toast. length_long更长的长度?


当前回答

用户不能自定义吐司的持续时间。因为NotificationManagerService的scheduleTimeoutLocked()函数没有使用字段duration。源代码如下。

private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
    {
        Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
        long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
        mHandler.removeCallbacksAndMessages(r);
        mHandler.sendMessageDelayed(m, delay);
    }

其他回答

使用Crouton,这是一个非常灵活的吐司库。

油煎面包块

你可以像烤面包一样使用它:

Crouton.makeText(context, "YOUR_MESSAGE", Style.INFO);

或者你甚至可以更深入一点,自定义更多,比如将时间设置为无限!例如,在这里我想显示一个toast消息,直到用户通过点击它来确认它。

private static void showMessage(final Activity context, MessageType type, String header, String message) {
    View v = context.getLayoutInflater().inflate(R.layout.toast_layout, null);
    TextView headerTv = (TextView) v.findViewById(R.id.toastHeader);
    headerTv.setText(header);
    TextView messageTv = (TextView) v.findViewById(R.id.toastMessage);
    messageTv.setText(message);
    ImageView toastIcon = (ImageView) v.findViewById(R.id.toastIcon);

    final Crouton crouton = getCrouton(context, v);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Crouton.hide(crouton);
        }
    });

    crouton.show();
}

private static Crouton getCrouton(final Activity context, View v) {
    Crouton crouton = Crouton.make(context, v);
    crouton.setConfiguration(new Configuration.Builder().setDuration(Configuration.DURATION_INFINITE).build());
    return crouton;
}

自定义布局,将膨胀为吐司。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:background="@drawable/shadow_container"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="@dimen/default_margin"
    tools:ignore="Overdraw">

    <ImageView
        android:id="@+id/toastIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/default_spacing_full"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/toastHeader"
            style="@style/ItemText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/toastMessage"
            style="@style/ItemSubText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

正如其他人提到的,Android toast可以是LENGTH_LONG或LENGTH_SHORT。这是没有办法的,你也不应该遵循发布的任何“技巧”。

toast的目的是显示“非必要的”信息,由于它们的持久影响,如果消息的持续时间超过一定的阈值,消息可能会脱离上下文。如果股票吐司被修改,使它们可以显示比LENGTH_LONG更长的时间,消息将在屏幕上停留,直到应用程序的进程终止,因为吐司视图被添加到WindowManager,而不是应用程序中的ViewGroup。我认为这就是硬编码的原因。

如果您确实需要显示toast样式的消息超过3.5秒,我建议构建一个附加到Activity内容的视图,这样当用户退出应用程序时,它就会消失。我的SuperToasts库处理这个问题和许多其他问题,请随意使用它!您很可能对使用SuperActivityToasts感兴趣

我知道答案已经很晚了。我也有同样的问题,在研究了android的Toast源代码后,决定实现我自己的裸骨吐司版本。

基本上,您需要创建一个新的窗口管理器,并使用处理程序在所需的持续时间内显示和隐藏窗口

 //Create your handler
 Handler mHandler = new Handler();

//Custom Toast Layout
mLayout = layoutInflater.inflate(R.layout.customtoast, null);

//Initialisation 

mWindowManager = (WindowManager) context.getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();

params.gravity = Gravity.BOTTOM
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = android.R.style.Animation_Toast;
params.type = WindowManager.LayoutParams.TYPE_TOAST;

初始化布局后,您可以使用自己的隐藏和显示方法

    public void handleShow() {
    mWindowManager.addView(mLayout, mParams);
    }

    public void handleHide() {
        if (mLayout != null) {
            if (mLayout.getParent() != null) {
                mWindowManager.removeView(mLayout);
            }
                         mLayout = null;
        }

现在你所需要的就是添加两个可运行的线程,分别调用handleShow()和handleHide(),你可以将它们发布到Handler中。

    Runnable toastShowRunnable = new Runnable() {
        public void run() {
            handleShow();
        }
    };

 Runnable toastHideRunnable = new Runnable() {
        public void run() {
            handleHide();
        }
    }; 

最后一部分

public void show() {

    mHandler.post(toastShowRunnable);
    //The duration that you want
    mHandler.postDelayed(toastHideRunnable, mDuration);

}

这是一个快速而肮脏的实现。没有考虑到任何业绩。

LONG_DELAY吐司显示为3.5秒,SHORT_DELAY吐司显示为2秒。

Toast内部使用INotificationManager并在每次调用Toast.show()时调用它的enqueueToast方法。

使用SHORT_DELAY调用show()两次将再次将同一toast排队。它将显示4秒(2秒+ 2秒)。

类似地,两次使用LONG_DELAY调用show()将再次将同一toast排队。它将显示7秒(3.5秒+ 3.5秒)

LENGTH_SHORT和LENGTH_LONG的值分别为0和1。这意味着它们被视为标志,而不是实际的持续时间,所以我认为不可能将持续时间设置为这些值以外的任何东西。

如果想要长时间地向用户显示消息,可以考虑使用状态栏通知。当状态栏通知不再相关时,可以通过编程方式取消它们。