Android--------自定義toast

佈局文件:java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

</LinearLayout>android

java代碼:佈局

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自定義Toast
 * @author pad
 *
 */
public class CustomToast {
    private Context context;
    public CustomToast(Context context){
        this.context=context;
    }
    /**
     * 顯示toast
     * @param message
     * @param inLayout
     */
    void showToast(String message,LayoutInflater layoutInflater){
        View view=layoutInflater.inflate(R.layout.toast_ui, null);
        TextView text=(TextView)view.findViewById(R.id.text);
        text.setText(message);
        Toast toast=new Toast(context);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);//設置toast出現的位置
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }

}ui

相關文章
相關標籤/搜索