Toast的几种类型

package com.jj.toast_activity;


/**
 * Created by jj on 2017/11/30.
 */


public class Student {
   public int img;
   public   String name;

}



package com.jj.toast_activity;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Created by jj on 2017/11/30.
 */


public class ToastUtils {
    private Context Mcontext;
    private Toast toast;


    public ToastUtils(Context context,int layout,Student student,int gravity){
        Mcontext=context;
        View view = LayoutInflater.from(context).inflate(layout, null);
        ImageView img= (ImageView) view.findViewById(R.id.show_iv);
        TextView tv =(TextView) view.findViewById(R.id.show_tv);


        img.setImageResource(student.img);
        tv.setText(student.name);


        toast=new Toast(context);
        toast.setView(view);
        toast.setGravity(gravity,0,0);
        toast.setDuration(Toast.LENGTH_SHORT);
    }
    public void show(){
        toast.show();
    }
}


package com.jj.toast_activity;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void onClick(View view) {
        switch (view.getId()){
            case R.id.box1:
                Toast.makeText(this, "普通消息", Toast.LENGTH_SHORT).show();
                break;
            case R.id.box2:
                Toast toast = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
                toast.show();
                break;
            case R.id.box3:
                Toast toast1 = Toast.makeText(this, "顶部Toast", Toast.LENGTH_SHORT);
                toast1.setGravity(Gravity.TOP,0,0);
                toast1.show();
                break;
            case R.id.box4:
                Toast toast2 = Toast.makeText(this, "图片Toast消息", Toast.LENGTH_SHORT);
                View view1 = getLayoutInflater().inflate(R.layout.mytoask, null);
                toast2.setView(view1);
                toast2.show();


                Student student=new Student();
                student.img=R.mipmap.ic_launcher;
                student.name="黄盖";
                ToastUtils toastUtils=new ToastUtils(this,R.layout.mytoask,student,Gravity.CENTER);
                toastUtils.show();
                break;
            case R.id.box5:
                Student student1=new Student();
                student1.img=R.mipmap.ic_launcher;
                student1.name="时间";
                ToastUtils toastUtilss=new ToastUtils(this,R.layout.mytoask,student1,Gravity.TOP);
                toastUtilss.show();
                break;
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jj.toast_activity.MainActivity">
    <Button
        android:id="@+id/box1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClick"
        android:text="普通Toast消息"/>
    <Button
        android:id="@+id/box2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClick"
        android:text="居中Toast消息"/>
    <Button
        android:id="@+id/box3"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClick"
        android:text="顶部Toast消息"/>
    <Button
        android:id="@+id/box4"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClick"
        android:text="图片Toast消息"/>
    <Button
        android:id="@+id/box5"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:onClick="onClick"
        android:text="改变位置的图片Toast消息"/>
</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/show_iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show_tv"
        android:text="龙门客栈"
        android:textSize="20sp"
        android:layout_marginLeft="13dp"/>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值