刚刚用到了Toast这个控件,有一些地方不明白,在网上找到这篇文章,转过来看看,如涉及版权问题,请联系我删除之。
转自:http://www.androidx.org/2009/10/21/android-custom-toast/
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。
首先建立一个ToastExample的项目,放置3个按钮,分别为Text Only,Icon Only,Text and Icon。布局及XML如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:text="Text Only" android:id="@+id/Button01" android:layout_width="100px" android:layout_height="wrap_content" /> <Button android:text="Icon Only" android:id="@+id/Button02" android:layout_width="100px" android:layout_height="wrap_content" /> <Button android:text="Icon and Text" android:id="@+id/Button03" android:layout_width="120px" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> |
然后在程序中为三个按钮分别创建点击事件,代码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Button btn
;
btn = ( Button ) findViewById (R. id. Button01 ) ; btn. setOnClickListener ( new Button. OnClickListener ( ) { public void onClick ( View v ) { Toast toast = Toast. makeText (getApplicationContext ( ), "Text toast test!", Toast. LENGTH_LONG );
toast.
show
(
)
;
} } ) ; btn = ( Button ) findViewById (R. id. Button02 ) ; btn. setOnClickListener ( new Button. OnClickListener ( ) { public void onClick ( View v ) { Toast toast = new Toast (getApplicationContext ( ) ) ; ImageView view = new ImageView (getApplicationContext ( ) ) ; view. setImageResource (R. drawable. ic_dialog_alert ) ; toast. setView (view ) ; toast. show ( ) ; } } ) ; btn = ( Button ) findViewById (R. id. Button03 ) ; btn. setOnClickListener ( new Button. OnClickListener ( ) { public void onClick ( View v ) { Toast toast = Toast. makeText (getApplicationContext ( ), "Text and Icon test!", Toast. LENGTH_LONG ) ; View textView = toast. getView ( ) ; LinearLayout lay = new LinearLayout (getApplicationContext ( ) ) ; lay. setOrientation (LinearLayout. HORIZONTAL ) ; ImageView view = new ImageView (getApplicationContext ( ) ) ; view. setImageResource (R. drawable. ic_dialog_alert ) ; lay. addView (view ) ; lay. addView (textView ) ; toast. setView (lay ) ; toast. show ( ) ; } } ) ; |
然后运行程序分别点击三个按钮可以看到三种不同情况下的Toast。
可以看到,最后一种图片加文字的显示显然是不符合我们的要求的,那么我们可以通过增加Toast的Layout来调整Toast上的布局方式。我们增加一个Toast的Layout描述xml文件,/res/layout/toast_layout.xml:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> </LinearLayout> |
然后我们修改Button3的OnClick事件:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
btn
=
(
Button
) findViewById
(R.
id.
Button03
)
;
btn. setOnClickListener ( new Button. OnClickListener ( ) { public void onClick ( View v ) { LayoutInflater inflater = getLayoutInflater ( ) ; View layout = inflater. inflate (R. layout. toast_layout, (ViewGroup ) findViewById (R. id. toast_layout_root ) ) ; ImageView image = (ImageView ) layout. findViewById (R. id. image ) ; image. setImageResource (R. drawable. ic_dialog_alert ) ; TextView text = (TextView ) layout. findViewById (R. id. text ) ; text. setText ( "Hello! This is a custom toast!" ) ; Toast toast = new Toast (getApplicationContext ( ) ) ; toast. setDuration (Toast. LENGTH_LONG ) ; toast. setView (layout ) ; toast. show ( ) ; } } ) ; |
再运行程序,显示如下:
另外,我们还可以使用setGravity()方法来定位Toast在屏幕上的位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。