美文网首页
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的作用和使用方法

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