美文网首页
Toast的作用和使用方法

Toast的作用和使用方法

作者: _春夏秋冬 | 来源:发表于2018-12-18 10:12 被阅读0次

Toasts
Toast API
android Toast大全(五种情形)建立属于你自己的Toast

toast.PNG

1.作用

Toast 提供有关操作的简单反馈,它以小窗口的形式展示。


2.使用方法

2.1 默认方法

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

还可以用如下方法:

Toast.makeText(MainActivity.this, "默认吐司", Toast.LENGTH_SHORT).show();

2.2 自定义显示位置

主要通过以下两个 API 实现

setGravity(int gravity, int xOffset, int yOffset);
setMargin(float horizontalMargin, float verticalMargin);

代码如下:

Toast toast = Toast.makeText(this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

2.3 带图片

查看源码

View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);

可知,Toast 自带的布局是 transient_notification.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginHorizontal="24dp"
        android:layout_marginVertical="15dp"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/primary_text_default_material_light"
        />

</LinearLayout>

我们可以得到父布局对象,并添加其他视图。

代码如下:

Toast toast = Toast.makeText(this, "带图片", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout ll = (LinearLayout) toast.getView();
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.btn_circle_pressed);
ll.addView(iv, 0);
toast.show();

2.4 完全自定义视图

代码如下:

View layout = LayoutInflater.from(this).inflate(R.layout.layout_toast, null);
TextView tvTop = layout.findViewById(R.id.tv_toast_content);
ImageView ivCenter = layout.findViewById(R.id.iv_toast);
TextView tvBottom = layout.findViewById(R.id.tv_toast);
tvTop.setText("Custom");
ivCenter.setImageResource(R.drawable.btn_circle_pressed);
tvBottom.setText("Custom View");
Toast toast = new Toast(this);
toast.setView(layout);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 0, 0);
toast.show();

或者使用代码布局也行,代码如下:

LinearLayout ll = new LinearLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.btn_circle_pressed);
TextView tv = new TextView(this);
tv.setText("自定义视图");
tv.setTextColor(Color.RED);
tv.setTextSize(20L);
ll.setGravity(Gravity.CENTER_VERTICAL);
ll.addView(iv);
ll.addView(tv);

Toast toast = new Toast(this);
toast.setView(ll);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

相关文章

  • Toast的作用和使用方法

    ToastsToast APIandroid Toast大全(五种情形)建立属于你自己的Toast 1.作用 To...

  • 使用JavaScript实现Toast小组件

    Toast的作用 Toast在很多系统中都存在,特别是在一些后台系统和手机端特别常见。通常在后台系统中,Toast...

  • iOS开发Toast提示框

    HCToast源码地址 Toast 提示框控件 使用方法: #import "HCToast.h"

  • ios · Toast提示框

    -松小宝- HCToast 源码地址 :HCToast github下载 Toast 提示框控件 使用方法: #...

  • snackbar

    介绍与Toast类似,但不是Toast的替代品,Toast的作用是告诉用户现在发生了什么事情,但是用户只能被动的接...

  • Android 高级自定义Toast及源码解析

    Toast概述 Toast的作用 不需要和用户交互的提示框。 更多参见官网:https://developer.a...

  • DRF 10大组件作用、使用方法

    认证 作用: 检测用户是否登录 使用方法 权限 作用: 某些接口只能是特定的用户才能访问 使用方法 节流 作用: ...

  • DRF 十大组件 作用

    认证 作用: 检测用户是否登录 使用方法 权限 作用: 某些接口只能是特定的用户才能访问 使用方法 节流 作用: ...

  • 精油的运用

    不同精油作用不同的,使用方法也不同的!比如,你可以看看下面一些精油的使用方法和作用: 一、对付黑头: 勤洗脸:只要...

  • 一文搞懂液压蓄能器的工作原理,作用和使用方法

    一文搞懂液压蓄能器的工作原理,作用和使用方法

网友评论

      本文标题:Toast的作用和使用方法

      本文链接:https://www.haomeiwen.com/subject/yvrkkqtx.html