StupidBeauty
Read times:1840Posted at: - no title specified

安卓开发文档翻译:吐息,Toasts

吐息(toast),是指一个小小的弹出框,它提供的是针对某个操作的简单的反馈。它只占用消息体本身所需的空间,而当前活动仍然保持可见并且接受与用户的交互。例如,在将一封邮件发送出去之前就切换到别的界面的话,就会触发一个“已保存草稿”吐息,以让妳知道,妳还可以在日后继续编辑它。吐息会在一定时间之后自动消失。

如果 某条消息是需要用户作出回应的,则,请考虑使用 通知

基本用法

首先 ,使用适当的 makeText() 方法来初始化一个 Toast 对象。 这个方法需要3个参数:应用程序上下文( Context )、文字内容、和吐息的持续时间。 它会返回一个已经初始化的吐息。 妳可以调用 show() 方法以显示这个吐息,如以下代码所示:

Context context = getApplicationContext ();

CharSequence text = "Hello toast!" ;

int duration = Toast . LENGTH_SHORT ;

Toast toast = Toast . makeText ( context , text , duration );

toast . show ();

这个示例,展示了对于大部分吐息所需要的代码。妳几乎不需要写些别的什么。不过呢,妳可能想要做些小调整:将吐息放置到不同的位置;或者使用妳自定义的布局,而不是简单的文字消息。后续小节将说明如何做这些调整。

妳还可以连锁调用这些方法,以避免在代码里显式地写一个Toast 对象,如下所示:

Toast . makeText ( context , text , duration ). show ();

将吐息放置在不同的位置

标准 的吐息,会出现在屏幕底部,水平方向上是居中的。 妳可以使用 setGravity(int, int, int) 方法来改变它的位置。 这个方法需要3个参数: 一个 Gravity 常量 、一个 x方向偏移 值、和一个 y方向偏移值。

例如,假设,妳想要让这个吐息显示在左上角,那么,可以这样写:

toast . setGravity ( Gravity . TOP | Gravity . LEFT , 0 , 0 );

如果妳想让它稍微向右边移动一点,那么,增加第二个参数的值。想让它稍微向下移动一点,那么,增加第三个参数的值。

创建一个自定义的吐息视图

如果简单 的文字消息无法满足要求的话,那么, 妳可以为吐息提示创建一个自定义的布局。 要想创建一个自定义的布局,则,使用XML 或程序代码来定义一个视图(View)布局,然后调用 setView(View) 方法,传入这个视图布局的根视图( View )对象。

例如, 妳可以使用以下XML(保存 toast_layout.xml )来为吐息创建一个自定义布局:

<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 = "8dp"

android:background = "#DAAA"

>

<ImageView android:src = "@drawable/droid"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:layout_marginRight = "8dp"

/>

<TextView android:id = "@+id/text"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:textColor = "#FFF"

/>

</LinearLayout>

注意,这个LinearLayout 的资源编号(ID)是"toast_layout_root"。妳必须使用这个编号来从 XML 中构造出布局,如下所示:

LayoutInflater inflater = getLayoutInflater ();

View layout = inflater . inflate ( R . layout . custom_toast ,

( ViewGroup ) findViewById ( R . id . toast_layout_root ));

TextView text = ( TextView ) layout . findViewById ( R . id . text );

text . setText ( "This is a custom toast" );

Toast toast = new Toast ( getApplicationContext ());

toast . setGravity ( Gravity . CENTER_VERTICAL , 0 , 0 );

toast . setDuration ( Toast . LENGTH_LONG );

toast . setView ( layout );

toast . show ();

首先 ,使用 getLayoutInflater() getSystemService() )来获取到 LayoutInflater ,然后 ,使用 inflate(int, ViewGroup) 来从 XML 中构造出布局。第一 个参数是布局的资源编号,第二个参数是根视图(View)。 妳可以使用这里构造出来的布局来寻找到该布局中更多的视图(View)对象,所以 ,妳现在应当找到对应 的ImageView 和TextView 元素,并且定义它们的内容。最后 ,使用 Toast(Context) 来创建一个新的Toast,并且设置该吐息的一些属性,例如重力属性和持续时间。然后调用 setView(View) ,并且传入刚才构造的布局。 好了,妳可以调用 show() ,以显示出拥有妳自定义布局的吐息了。

注意 除非 妳是准备使用 setView(View) 来定义它的布局,否则的话,不要使用Toast 的公有构造函数。如果 妳没打算使用一个自定义布局的话,则必须使用 makeText(Context, int, int) 来创建该Toast。

未知美人

Your opinions

Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands