Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串对于Toast太长-如何显示字符串的结尾而不是开头_Java_Android_Android Studio - Fatal编程技术网

Java 字符串对于Toast太长-如何显示字符串的结尾而不是开头

Java 字符串对于Toast太长-如何显示字符串的结尾而不是开头,java,android,android-studio,Java,Android,Android Studio,好的,这可能是一个非常简单的问题。我有一个字符串值要显示在toast中。例如 String tempstring = "qwertyuiopasdfghjklzxcvbnm123456789012345678901234567890"; toast = Toast.makeText(getApplicationContext(), tempstring , Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.sh

好的,这可能是一个非常简单的问题。我有一个字符串值要显示在toast中。例如

String tempstring = "qwertyuiopasdfghjklzxcvbnm123456789012345678901234567890"; 
toast = Toast.makeText(getApplicationContext(), tempstring , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

我的问题是,我的程序中的tempstring通常会变得非常大,而且文本量有时会超过toast的显示量。当这种情况发生时,土司将显示从开始到结束的文本。我希望祝酒词始终显示字符串的结尾,但如果文本太多而无法在祝酒词中一次性显示,则应切掉开头。请问有什么简单的方法吗?

这是您需要的。它是在gmail应用程序中实现的

因此,您可以自定义toast布局,根据需要使用任意多的行,而无需
甚至切东西。

好的,我自己解决了这个问题

这不太难

答案是为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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="horizontal"
android:padding="5dp"

>

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"
    android:gravity="bottom"
    />
</LinearLayout>

工作完成了

也许有更好的方法,但为什么不先确定toast的最大字符串长度,然后将tempstring长度与之比较,然后使用子字符串切割开头,并仅显示toast中的剩余部分?缺点-每次您都需要比较字符串长度并使用子字符串…谢谢,但是如何在考虑不同大小屏幕的情况下确定吐司的最大字符串长度?正如@Melquiades所说,您可以使用类似以下内容;tempstring=tempstring.length()@regnombious,我不确定,但您可以在几个不同的分辨率设备(或模拟器)上进行测试,看看是否存在模式…如果您想对代码添加一些解释,可以使用另一行。您还可以在添加评论的同时复制和粘贴更有趣的部分。
    LayoutInflater inflater = getLayoutInflater();

    View layout = inflater.inflate(R.layout.custom_toast,
            (ViewGroup) findViewById(R.id.custom_toast_layout_id));
    // set a message
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText(themessage);

    toast = Toast.makeText(getApplicationContext(),
                    themessage, Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();