一、android Toast是一個很不錯的信息提示工具,這裏咱們實現了莪一個自定義Toast,使提示信息更加生動起來,看一下Toast的佈局文件代碼:java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@null" > <RelativeLayout android:layout_width="680px" android:layout_height="400px" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:background="@drawable/bg" android:gravity="center" android:orientation="vertical" android:paddingBottom="37dp" android:paddingTop="39dp" > <TextView android:id="@+id/tf_msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="center" android:singleLine="true" android:textColor="#ffffff" android:textSize="25sp" /> </RelativeLayout> </LinearLayout>
二、 顯示效果:android
三、代碼中的應用:工具
/** * 顯示信息 * * @param msg */ private void showToast(String msg) { LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService( LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.tf_toast, null); TextView textView = (TextView) view.findViewById(R.id.tf_msg); textView.setText(msg); Toast toast = new Toast(MainActivity.this); toast.setDuration(Toast.LENGTH_LONG);//信息顯示時間 toast.setView(view); toast.setGravity(Gravity.CENTER, 0, 0);//信息顯示位置,這裏在中間位置 toast.show(); }