Android之Toast的用法
Android之Toast的用法
有些程序可以配置,用来让用户设置有些自定义的偏好
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图片如下所示
评论暂时关闭