与你日常的标准回答打印敬酒之前,请阅读问题:)
我想在屏幕的左上角显示自定义吐司。我用这个code创建吐司:
I'd like to display a Custom Toast at the top left corner of the screen. I use this code to create the 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个像素的边距。 许多其他设备正常工作。
However, in some devices, for example Samsung Galaxy S4, the toast is not positioned at (0,0), but with a margin like 40-50 pixels. Many other devices work as expected.
我肯定保证金由窗口管理器添加(烤面包片视图添加类型TYPE_TOAST到窗口管理视图)
I am positive the margin is added by the WindowManager (The toast view is added as a View of type TYPE_TOAST to the WindowManager)
这是为什么?它可以修改?请参阅code以下,我已经克隆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);
因此,它看起来就像是谁在添加这些设备在这个保证金的窗口管理器。看起来像一个安全区或类似的东西,但我不能找到在哪里(如果)这是可以改变的。
So it looks like it's the WindowManager who is adding this margin on those devices. Looks like a safe area or something like that, but I cant find where (or if) this can be changed.
帮助AP preciated。
Help appreciated.
我想通了这一点。
开始4.4+必须添加以下标志:
Starting 4.4+ you have to add the following flag:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
mParams.flags = mParams.flags | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
}
这将使你敬酒要一直放置在上面。
This will allow your toast to be positioned all the way to the top.