自定义Tosat

    1、android Toast是一个很不错的信息提示工具,这里我们实现了莪一个自定义Toast,使提示信息更加生动起来,看一下Toast的布局文件代码:

<?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>

   2、 显示效果:

    自定义Tosat

    3、代码中的应用:

/**
  * 显示信息
  * 
  * @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();
 }

你可能感兴趣的:(自定义Tosat)