自定義吐司

我叫阿狸貓發表於2014-07-21
View view = View.inflate(this, R.layout.number_address_toast, null);//左邊是電話圖片,右邊是歸屬地資訊的佈局檔案
view.setBackgroundResource(bgs[numberAddressBgWhich]);//也可以是一個TextView,bgs是一個背景圖片的陣列  numberAddressBgWhich是從SharedPreferences裡讀取的
TextView tv_number_address = (TextView) view.findViewById(R.id.tv_number_address);
tv_number_address.setText(address);//設定右邊的歸屬地資訊 
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
// 窗體的引數就設定好了
WindowManager.LayoutParams params = new WindowManager.LayoutParams();

params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;

params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
		| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
		| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(view, params);//引數一:自定義吐司裡要顯示的東西   引數二:窗體管理器

主要就是建立一個佈局,這個佈局可以是載入佈局檔案,也可是隻是一個TextView,然後用WindowManager.addView(view,params);即可   第二個引數就是窗體



佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_call" />

    <TextView
        android:id="@+id/tv_number_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="號碼歸屬地"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>



相關文章