美文网首页
Toasts (吐司)

Toasts (吐司)

作者: 王世军Steven | 来源:发表于2016-11-28 08:45 被阅读40次

Toasts

吐司通过弹出框对用户的操作进行简单反馈. 吐司只会占据屏幕的一小部分空间,并且当前的
Activity依然是可见和可交互的状态.

基本用法

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

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

// 常用一下写法
Toast.makeText(context,text,duration).show();

Positioning your Toast

一个标准的toast显示在屏幕下方水平居中的位置. 我们可以通过使用setGravity(int,int,int)
方法设置显示位置.该方法接收三个参数.一个Gravity常量.一个x轴方向偏移量和一个y轴方向偏移量.

// 增加第二个参数可以实现右移.
// 增减第三个参数可以实现下移.
toast.setGravity(Gravity.TOP|Gravity.LEFT,0,0);

Creating a Custom Toast View .

可以创建一个布局文件,然后将布局文件对象作为toast.setView(View)方法的参数,这样就可以实现吧自定义布局的Toast.

自定义布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:id="@+id/toast_layout_root"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#DAAA"
              android:padding="8dp">
    <ImageView
        android:layout_width="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_marginEnd="8dp"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/tv_txt"
        android:textColor="#FFF"
        android:layout_height="wrap_content"/>
</LinearLayout>

引用布局文件

    public void showCustomToast(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root));

        TextView txt = (TextView) layout.findViewById(R.id.tv_txt);
        CharSequence text = "Custom Toast View";
        txt.setText(text);

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL ,0,0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

相关文章

  • Toasts (吐司)

    Toasts 吐司通过弹出框对用户的操作进行简单反馈. 吐司只会占据屏幕的一小部分空间,并且当前的Activity...

  • Material design - Components– Sn

    Snackbars & toasts Snackbars provide brief feedback about...

  • Snackbar的使用

    参考 Snackbars 与 Toasts放弃Toast吧,用SnackbarSnackbar样式Snackbar...

  • Toasts:便利贴、不干胶和牛皮癣

    上一次是无聊吧啦了一下面包屑和烤面包,今儿认真的来讨论一下Toasts。 Toasts是一位绅士,它每次的出现都希...

  • 吐司控的花样吃法

    ​作为一个面包控,准确的说是吐司控,无论出行到哪里,总会抱着一堆吐司来啃。 原味吐司、提子吐司、抹茶吐司、杂粮吐司...

  • 吐司村

    今天和王翎芳老师交流。她想在平田村做一个吐司村,用道地食材研发美食,如制作吐司,就有红糖吐司、抹茶吐司、蓝莓吐司…...

  • Android 自定义Toast

    前言 系统吐司太丑 之前吐司都是自定义View去做吐司 但是今天才发现原来有人写好的轮子下面呢这个就是吐司依赖 既...

  • 2018-07-30上市3款吐司说明

    2018年07月30日上市3款吐司 分别位于输入分组的:面包 这三款吐司为: 奶酥吐司 15元 裸麦水果吐司15元...

  • 花式吃吐司1——热狗吐司卷

    切片吐司是最朴实方便的早餐主食。天天吐司片,总会腻,所以,我们试试花式吃吐司。 今天早餐做的是热狗吐司卷。切片白吐...

  • 面包新创意,猫咪吐司出没,简直萌出新天际,回头率高达99%

    吐司,虽然种类繁多,除了有不同的做法外,还有着山型吐司和方型吐司之说,但这款三色猫咪吐司,是不是没听过? 喏,就是...

网友评论

      本文标题:Toasts (吐司)

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