Simple Android toasts not aligning properly

后端 未结 2 650
南笙
南笙 2020-12-17 14:55

I\'m simply calling from my Activity:

Toast.makeText(this, \"This is a toast\", Toast.LENGTH_SHORT).show()

But the result is a

2条回答
  •  一整个雨季
    2020-12-17 15:39

    This situation occurs because in the horizontal screen state, the width of the navigation bar will also be calculated, regardless of whether the navigation bar is hidden or not, so you can subtract half the width of the navigation bar.

    toast.setGravity(Gravity.CENTER, getNavigationBarHeight() / 2, 0); 
    
        private int getNavigationBarHeight() {
            Resources resources = this.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                return resources.getDimensionPixelSize(resourceId);
            }
            return 0;
        }
    

提交回复
热议问题