Toasts

来源:互联网 发布:2016年宏观经济数据 编辑:程序博客网 时间:2024/06/06 10:03

简单的Toast:Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();
还可以设置Toast出现的位置:toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show();

自定义Toast

首先我们先定义一个布局文件toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/toast_layout_root">    <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>

在javadiamagnetic中设置:

private void displayToast(String message) {  // 加载布局文件  View layout = getLayoutInflater().inflate(R.layout.toast_layout,                               (ViewGroup) findViewById(R.id.toast_layout_root));  // 向toast中填入信息  TextView text = (TextView) layout.findViewById(R.id.text);  text.setText(message);   // Construct the toast, set the view and display  Toast toast = Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT);  toast.setView(layout);  toast.show();}
0 0