【问题标题】:Create a Toast that Comes From Top of Screen创建来自屏幕顶部的 Toast
【发布时间】:2016-03-30 15:10:15
【问题描述】:

我想知道我们如何在 Android 中创建自定义 toast,像这样显示在屏幕顶部?

我来自 iOS 背景,现在我必须创建这样的自定义控件。

请指点一下?

谢谢

【问题讨论】:

标签: android android-layout android-custom-view toast android-library


【解决方案1】:

您可以为此目的使用 crouton

说明

Crouton 将显示在开发人员决定的位置。 标准将是应用程序窗口的顶部。你可以排队 多个Crouton供展示,一个接一个展示

为任何 CharSequence 创建一个面包块:

Crouton.makeText(Activity, CharSequence, Style).show();

more details on Crouton适用于 Android 的上下文相关通知

有很多你可以用来烤面包描述here

【讨论】:

    【解决方案2】:

    如您所见,有很多解决方案。但最简单的方法是在您的 XML 布局文件中创建一个布局,并使其不可见。当需要显示此视图时,使其可见并启动动画。

    【讨论】:

    • 请包含一些代码以显示 OP 如何实施此建议。
    【解决方案3】:

    查看此链接:https://github.com/gfranks/GFMinimalNotifications,这是您想要的,我认为它对我来说很好。

    输出:

    或者您可以像这样准备自定义吐司:

    View layout = getLayoutInflater().inflate(R.layout.customtoast,
                (ViewGroup) findViewById(R.id.custom_toast_layout));
    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP, 0, 0);
    toast.setView(layout);
    toast.show();
    

    【讨论】:

      【解决方案4】:

      创建自定义 toast 布局,toast.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:padding="8dp"
            android:gravity="center"
            android:textSize="32sp"
            android:layout_height="256dp"/>
      

      使用上面的布局创建 toast:

          static Toast t;
      
      public static void show(Context c, String s) {
          if (t == null) {
              t = new Toast(c.getApplicationContext());
              t.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
              LayoutInflater inflater = LayoutInflater.from(c);
              TextView v = (TextView) inflater.inflate(R.layout.toast, null);
              t.setView(v);
              t.setDuration(Toast.LENGTH_LONG);
          }
          TextView v = (TextView) t.getView();
          v.setText(s);
          t.show();
      }
      

      【讨论】:

        【解决方案5】:

        试试这个 ::

        Toast toast= Toast.makeText(getApplicationContext(), 
        "Your string here", Toast.LENGTH_SHORT);  
        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
        

        为位置添加这一行

         toast.setGravity(Gravity.TOP, 0, 0);
        

        【讨论】:

          猜你喜欢
          • 2021-10-10
          • 2012-12-14
          • 2012-09-06
          • 2015-04-09
          • 1970-01-01
          • 1970-01-01
          • 2011-12-25
          • 1970-01-01
          • 2010-11-16
          相关资源
          最近更新 更多