Toast的简单用法

1,什么是Toast?

@1,Toast是一种提供给用户简洁提示信息的视图。

@2,这个视图是浮在应用程序之上的,toast的提示是不获取焦点的,所以她不会影响用户的操作,简单来说就是不影响用户使用的同时,给用户提供某些提示信息,比如音量控 制,再按一次推出程序等都是toast的应用。

@3,Android提供了Toast类可以创建和显示Toast信息。

2,Toast的通常用法?

@1,简单的用法

Toast.makeText(ChatActivity.this, “我是简单的Toast”, Toast.LENGTH_SHORT).show();//这个是最简单的用法

context上下文,一般是类名.this;fragment里传getActivity;

text就是要显示的内容;

duration就是显示的时间长短,可以传0或者1,也可以传Toast.LENGTH_SHORT,相当于传0;
Toast.LENGTH_LONG,相当于传1;


@2,自定义显示位置的Toast
Toast toast = Toast.makeText(getApplicationContext(),
“自定义位置Toast”, Toast.LENGTH_LONG);
toast.setGravity(gravity,xOffset,yOffset);//设置Toast的位置
toast.show();//显示Toast可以传
Gravity.CENTER_HORIZONTAL;(水平居中),Gravity.CENTER_VERTUCAL(垂直居中)
Gravity.TOP(最上面),Gravity.BOTTOM(最下面)等等
xOffset,yOffset这两个参数是水平和垂直的偏移量,


@3.带图片的Toast
Toast toast = Toast.makeText(getApplicationContext(),
“带图片的Toast”, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);//设置显示位置
LinearLayout toastView = (LinearLayout) toast.getView(); //获得toast的布局
ImageView image = new ImageView(Context);//创建图片控件
image.setImageResource(R.drawable.icon);//给控件设置图一个位置
toastView.addView(imageCodeProject, 0);//将ImageView在加入到此布局中的第一个位置
toast.show();//显示toast


@4,自定义的Toast
首先创建一个布局文件
`

“`
veLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<TextView
    android:id="@+id/tvTitleToast"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="这里是标题" />

<LinearLayout
    android:id="@+id/llToastContent"
    android:layout_below="@+id/tvTitleToast"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="1dip"
    android:layout_marginLeft="1dip"
    android:layout_marginRight="1dip"
    android:background="#00ff00"
    android:orientation="vertical"
    android:padding="15dip" >

    <ImageView
        android:id="@+id/tvImageToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tvTextToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="这里是toast的内容" />
</LinearLayout>

`

引入布局
View layout = View.inflate(MainActivity.this,R.layout.activity_main2,null);
//实例化图片控件
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
//设置布局中图片视图中图片
image.setImageResource(R.drawable.ic_launcher);
//实例化textView控件
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
//设置标题
title.setText(“标题栏”);

   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
   text.setText("自定义Toast");  //设置内容  

Toast toast= new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER , 100, 0); //设置位置
toast.setDuration(Toast.LENGTH_LONG); //设置时间
toast.setView(layout);//给Toast设置view
toast.show(); //显示Toast

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值