Toast 能动态显示信息,并且不获取焦点。
常用方法
- Toast.makeText(context,textt,duration);//返回值为Toast
- toast.setduration(duration);//设置显示时间
- toast.setGravity(gravity,x0ffset,y0ffset);//设置toast位置
- 其中gravity有许多默认的参数可以用,第二个和第三个参数分别是x和y的偏移量
- toast.setText(s);//设置显示内容
- toast.show();//显示
实现带有图片的Tosat:
通过使用
toast.getView()的方法
example:
-------------------------------------------------------------------------------------------------------------------------
LinearLayout toast_layout = (LinearLyaout)toast.getView();
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.*****);
toast_layout.addView(iv,0 );
toast.show();
自定义Toast
example:
-------------------------------------------------------------------------------------------------------------------------
//先做创建布局.xml文件
//使用了线性布局
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text=" 自定义Toast"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/topimg"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="内容"
/>
//main_activity中的方法
private void showToast(){
LayoutInflater inflater = LayoutInflater.from(this);
View toast_view = inflater.inflate(R.layout.****,null);
//将布局文件转换成 VIew对象
Toast toast = new Toast(this);
toast.setView(view);
toast.show();
}