android toast 不适合文本

发布于 2024-11-27 02:01:41 字数 242 浏览 1 评论 0原文

我正在开发一个应用程序,我必须使用大量的吐司。

我通过使用以下方式显示这些 toast:

Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show();

但是,显示器 toast 的高度为一行,而文本显示在多行上。结果是我无法查看toast中的所有文本。

我该如何解决这个问题?

I am developing an application where I have to use numerous toasts.

I display these toasts by using:

Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show();

The displayer toast, however, has the height of one line, while the text is displayed on multiple lines. The result is that I can't view all the text in the toast.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的心境我的脸 2024-12-04 02:01:41

尝试在要拆分文本的位置插入回车符和换行符。

这些字符指的是旧打字机模型。回车是指气缸回到起点,换行是指气缸滚动(进给)一行。

在计算中,它们由两个转义字符表示(特殊代码,通过在字符串中添加反斜杠 \ 前缀,允许在字符串中使用不可打印的代码)。

  • 回车由 \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 中,您应该能够将其减少为新行字符 < code>\n 因为基于 UNIX 的系统并不那么挑剔:

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

Try inserting a carriage-return and line-feed where you want to split the text.

These characters refer back to the old typewriter models. Carriage return was the cylinder moving back to the start and line feed was the cylinder rolling (feeding) by one line.

In computing these are represented by two escaped characters (special codes that allow non-printable codes inside a string by prefixing them with a backslash \).

  • A carriage return is represented by \r
  • A line-feed is represented by \n (you can remember this as a new line).

Some non-unix systems (e.g. Windows) require both, others (e.g Linux on which Android is based) only need the new line but it is generally safe to do both everywhere. The one thing that is essential is the order they are in. It must be \r\n

To put this into your example:

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

In Android you should be able to reduce this to just the new line character \n as unix based systems are not so fussy:

Toast.makeText(context, "First line of text\nSecond line of text", Toast.LENGTH_SHORT).show();
囍笑 2024-12-04 02:01:41

使用这个Android中的自定义toast:一个简单的例子的主要思想和这个 Android 的 Toast 默认颜色和 alpha 我开发了一个简单的自定义Toast 看起来与默认的 Toast 类似,但它将文本包裹在多行中。

我使用 makeText(context,text,duration) 静态方法创建了一个简单的类,因此我只需将 Toast.makeText 替换为 CustomToast.makeText code> 在我的项目中随处可见。

代码下方

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>

Using the main idea of this Custom toast in android : a simple example and this Android's Toast default colors and alpha I developed a simple custom Toast that looks like the default one but it wraps the text in multilines.

I create a simple class with the makeText(context,text,duration) static method, so I had only to replace Toast.makeText with CustomToast.makeText everywhere in my projects.

Below the code

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

}

The layout 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文