【问题标题】:how to set text new line in Toast at center android如何在中心android的Toast中设置文本换行
【发布时间】:2016-09-22 03:00:52
【问题描述】:

我的 Toast 有 3 行,我希望第 2 行和第 3 行显示此 toast 的中心并将持续时间设置为 10 秒。

怎么办?

喜欢这张照片:

【问题讨论】:

  • aaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc Toast.makeText(getApplicationContext(), getResources().getString(R.string.my_string_test), Toast.LENGTH_LONG) .show();

标签: center duration android-toast


【解决方案1】:

\n 里面的字符串有效!!!

Toast.makeText(this, "line1\nLine2", Toast.LENGTH_SHORT).show();

【讨论】:

  • 它可以添加一个新行,但它不会按照 OP 的要求将文本居中。
【解决方案2】:

您可以自定义 Toast 类。这是一个例子:

Context context=getApplicationContext();
        LayoutInflater inflater=getLayoutInflater();

        View customToastroot =inflater.inflate(R.layout.YOUR_TOAST_LAYOUT, null);

        Toast customtoast=new Toast(context);

        customtoast.setView(customToastroot);
        customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);
        customtoast.setDuration(Toast.LENGTH_LONG);
        customtoast.show();

更多详细教程请访问herehere

【讨论】:

    【解决方案3】:
    Toast.makeText(this,"YOUR MESSAGE",
                          +"\n"+
                         "Your other message",
    
    Toast.Lenght_long).show();
    

    每次用

    添加一行

    +"\n"+

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多