Android自定義Toast

加飯也加菜發表於2017-10-11

這是一個可以自定義Toast的UI的工具類。廢話少說,直接上程式碼。

package com.newtonapple.zhangyiyan.zhangyiyan.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newtonapple.zhangyiyan.R;

public class ToastUtils {

//直接顯示Toast
public static void show(Context context, String info) {

    View view = getTextView(context, info);
    Toast toast = new Toast(context);
    toast.setView(view);
    toast.show();

}

private static View getTextView(Context context,String info){
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.tv_toast, null);
    TextView tv = (TextView) view.findViewById(R.id.tv_toast);
    tv.setText(info);
    return view;
}

//設定Toast可以顯示多長時間
public static void show(Context context, String info, final long duration) {
    if (context == null)
    {
        return;
    }
    final Toast toast = Toast.makeText(context, info, Toast.LENGTH_SHORT);
    toast.show();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            toast.cancel();
        }
    }, duration);
}複製程式碼

//自定義樣式的Toast
public static void showZhengZaiKaiTong(Context context) {
if (context == null)
{
return;
}
final Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
//自定義Toast的佈局
View view = LayoutInflater.from(context).inflate(R.layout.layout_toast,null,false);
LinearLayout ll_toash = (LinearLayout) view.findViewById(R.id.toast);

    //佈局檔案中設定的寬高不頂用,需要重新設定;注意:不能設定最外層控制元件的寬高,會報空指標,可以設定第二層控制元件的寬高
    Activity activity = (Activity) context;
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();
    ll_toash.getLayoutParams().width = (int) (screenWidth*0.411);
    ll_toash.getLayoutParams().height = (int) (screenHeight*0.18);

    //設定吐司居中顯示
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setView(view);
    toast.show();
}

//自定義樣式的Toast
public static void yiShanChu(Context context) {
    if (context == null)
    {
        return;
    }
    final Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
    View view = LayoutInflater.from(context).inflate(R.layout.layout_toast_delete,null,false);
    LinearLayout ll_toash = (LinearLayout) view.findViewById(R.id.toast);
    //佈局檔案中設定的寬高不頂用,需要重新設定;注意:不能設定最外層控制元件的寬高,會報空指標,可以設定第二層控制元件的寬高
    Activity activity = (Activity) context;
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();
    ll_toash.getLayoutParams().width = (int) (screenWidth*0.411);
    ll_toash.getLayoutParams().height = (int) (screenHeight*0.18);

    //設定吐司居中顯示
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setView(view);
    toast.show();
}複製程式碼

}

相關文章