繁体   English   中英

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

[英]String is too long for Toast - How to display end of string rather than beginning

好的,这可能很简单。 我有一个要在烤面包中显示的字符串值。 例如...

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

我的问题是程序中的tempstring经常会变得很大,并且文本量有时会比吐司显示的内容大。 发生这种情况时,烤面包机将显示从头到尾的文字。 我希望烤面包机始终显示字符串的结尾,但如果文本太多而无法在烤面包机中一次显示,则要切开开头。 请问有简单的方法吗?

这就是你所需要的。 它在gmail应用中实现。

https://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java

这样一来,您就可以按照自己的意愿对吐司进行自定义布局,并且可以随意增加行数,甚至无需削减任何内容。

好的,我自己解决了。

不太难...

答案是为烤面包创建一个自定义视图。 首先创建一个像这样的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>

然后从您的烤面包上引用这样的视图...

    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();

任务完成...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM