您的位置:首页 > 移动开发 > Android开发

Android之Toast的用法

2011-09-26 09:31 204 查看
Toast是用来向用户展示一些提示性信息,并且该VIEW永远不会获得系统的焦点

一般定义为:Toast.makeText(MyClass.this,"note info",Toast.LENGTH_SHORT).show();

也可以在Tost中显示出自定义的VIEW,如下所示展示出图片和文字

自定义的 一个显示Toast的方法

protected void showToast() {
// create the view
View view = inflateView(R.layout.incoming_message_panel);

// set the text in the view
TextView tv = (TextView)view.findViewById(R.id.message);
tv.setText("khtx. meet u for dinner. cul8r");

// show the toast
Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}

incoming_message_panel的定义如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/toast_frame">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample_thumb_2"
/>

<TextView
android:id="@+id/message"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
/>

</LinearLayout>
</FrameLayout>


toast_frame图片如下所示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android layout encoding