安卓Toast不适合文本显示。

9
我是一个开发应用的人,需要使用大量的“Toast”提示信息。
我通过以下方式来显示这些提示信息:
Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show();
显示器的提示框只有一行的高度,而文本却需要多行来显示。因此,我无法查看提示框中的所有文本。
我该如何解决这个问题?
2个回答

31
尝试在你想要分割文本的位置插入回车符和换行符。
这些字符是旧式打字机的暗示。回车是指滚筒移回开头,而换行是指滚筒向下滚动(馈送)一行。
在计算机中,它们通过两个转义字符表示(特殊代码允许在字符串中使用非可打印字符,在它们前面加上反斜杠\)。
  • 回车由\r表示
  • 换行由\n表示(可以记为新一行)。
某些非Unix系统(例如Windows)需要两者,而其他系统(例如基于Android的Linux)仅需要新行,但通常在任何地方都要同时加。 唯一必不可少的是它们的顺序。 它必须是\r\n
将其应用于你的例子中:
Toast.makeText(context, "First line of text\r\nSecond line of text", Toast.LENGTH_SHORT).show();

在Android中,您应该能够将其简化为仅使用换行符\n,因为基于Unix的系统并不是很苛刻:

Toast.makeText(context, "First line of text\nSecond line of text", Toast.LENGTH_SHORT).show();

非常感谢您的帮助和详细的解释。现在吐司的尺寸已经适合文本了。 - Gabriel
如果 Toast 消息中的字符串来自 PHP echo,那么这个想法似乎行不通。例如,如果你有一个 echo "fist line\nsecond line",你的应用程序将显示一个带有 "fist linesecond line" 的 Toast 消息,这是一件糟糕的事情,不是吗? - JoeCoolman

1

利用这个Android中的自定义Toast:一个简单的例子和这个Android Toast默认颜色和透明度的主要思路,我开发了一个简单的自定义Toast,它看起来像默认的Toast,但可以将文本换行。

我创建了一个简单的类,并使用makeText(context,text,duration)静态方法,所以在我的项目中只需要将Toast.makeText替换为CustomToast.makeText即可。

以下是代码

CustomToast.java

public class CustomToast extends Toast{
    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public CustomToast(Context context) {
        super(context);
    }

    public static Toast makeText(Context context, CharSequence text, int duration) {
        Toast t = Toast.makeText(context,text,duration);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View layout = inflater.inflate(R.layout.custom_toast,null);

        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);

        t.setView(layout);


        return t;
    }

}

布局文件 layout/custom_toast.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/custom_toast_layout_id"
              android:background="@android:drawable/toast_frame"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal"
              android:gravity="center_horizontal|center_vertical"
              android:padding="5dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:layout_weight="1"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="@android:color/background_light"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"/>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接