天天看點

android toast列印,android – 螢幕頂部的自定義Toast

請在回答标準例程之前閱讀問題以列印Toast 🙂

我想在螢幕的左上角顯示自定義Toast.我使用此代碼建立Toast:

Toast mFixedToast = new Toast(getApplicationContext());

mFixedToast.setDuration(timeout);

mFixedToast.setView(myInflatedLayout);

mFixedToast.setGravity(Gravity.TOP|Gravity.FILL_HORIZONTAL, 0, 0);

mFixedToast.setMargins(0,0);

然而,在一些裝置中,例如三星Galaxy S4,吐司不是位于(0,0),而是具有40-50像素的邊緣.許多其他裝置按預期工作.

我很肯定WindowManager添加了邊距(Toast視圖作為TYPE_TOAST類型的視圖添加到WindowManager)

這是為什麼?可以修改嗎?請看下面的代碼,我已經将Toast.java克隆到我自己的類中,并隔離了将視圖添加到WM的行:

// LayoutParams for the TOAST view ... tried to change params.type but no luck.

final WindowManager.LayoutParams params = mParams;

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.windowAnimations = android.R.style.Animation_Toast;

params.type = WindowManager.LayoutParams.TYPE_TOAST;

mWM = (WindowManager)mView.getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

mParams.gravity = gravity;

// CHECKED these all are 0 !!!

mParams.x = mX; mParams.y = mY;

mParams.verticalMargin = mVerticalMargin;

mParams.horizontalMargin = mHorizontalMargin;

.

.

if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this+" with "+mX+","+mY+","+mVerticalMargin+","+mHorizontalMargin);

mWM.addView(mView, mParams);

是以看起來是WindowManager在這些裝置上添加這個邊距.看起來像一個安全區域或類似的東西,但我無法找到可以改變的地方(或者是否).

幫助贊賞.