`
韶华无限
  • 浏览: 10314 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Toast详解

XML 
阅读更多
之前只是看了API,没有测试,现在测试了一下,修改文章,呵呵。
Toast的显示我想大家也应该会的,这是我对toast的理解,不对之处请朋友们指出了。如下:
Context context = getApplicationContext(); 
CharSequence text = "我的Toast"; 
int duration = Toast.LENGTH_SHORT; //Toast的显示时间Toast.LENGTH_LONG
 
Toast toast = Toast.makeText(context, text, duration); 
toast.show();

或者也可以这样:
Toast.makeText(context, text, duration).show();


通常情况下,Toast都是在手机屏幕的底部显示的,我们也可以用 setGravity(int, int, int) 来自定义Toast的位置,第一个参数为Gravity,第二个和第三个参数是Toast的起点x,y坐标位置。如果想让Toast出现在屏幕的左上角位置,可以这样:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

其实我们也可以定义自己的Toast,下面讲解自己制作Toast的过程。如图:



首先定义一个名为mytoast的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/toast_loyout"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:padding="10dp"
	android:background="#DAAA"   
	>
<ImageView
	android:id="@+id/toast_Image"
	android:layout_width="40dip"
	android:layout_height="40dip"
	>
</ImageView> 
<TextView
   	android:id="@+id/toast_text"
   	android:layout_width="180dip"
   	android:layout_height="fill_parent"
   	android:background="#fff"
   	>
</TextView>
</LinearLayout>

然后定义Toast:
LayoutInflater inflater = getLayoutInflater(); //也可以用getSystemService().
View toastView = inflater.inflate(R.layout.mytoast, 
                               (ViewGroup) findViewById(R.id.toast_loyout)); 
 
ImageView image = (ImageView) toastView.findViewById(R.id.toast_Image); 
image.setImageResource(R.drawable.icon); 
TextView text = (TextView) toastView.findViewById(R.id.toast_text); 
text.setText("请在搜索框中输入您要搜索的歌曲或歌手"); 
 
Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
toast.setDuration(Toast.LENGTH_SHORT); 
toast.setView(toastView); 
toast.show();

这样就定义了自己的toast。
  • 大小: 20 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics