Попробуйте приведенное ниже решение.
Получите размер надреза для каждого устройства и установите верхний край поля для просмотра. Таким образом, он подходит для всех устройств.
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
this.getWindow().setStatusBarColor(Color.TRANSPARENT);
super.onCreate(savedInstanceState);
setContentView(MainActivity.this, R.layout.activity_main);
/*------------ Check display cutout size and give top margin to toolbar -------------*/
setMarginTopAccordingDisplayCutout(context, yourTopMainView, convertDpToPixel(34), 0);
/*-----------------------------------------------------------------------------------*/
...
//your Code
}
private void setMarginTopAccordingDisplayCutout(Context context, View view, int extraTopWithoutCutout, int extraTopWithCutout) {
int statusBarHeight = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
if (statusBarHeight > convertDpToPixel(24)) {
final ViewGroup.MarginLayoutParams[] layoutParams = new ViewGroup.MarginLayoutParams[1];
layoutParams[0] = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams[0].topMargin = statusBarHeight + extraTopWithCutout;
view.setLayoutParams(layoutParams[0]);
//topbarlp.setMargins(0, statusBarHeight, 0, 0);
//Set above layout params to your layout which was getting cut because of notch
///topbar.setLayoutParams(topbarlp)
Log.e(TAG, "onCreate statusBarHeight :: " + statusBarHeight);
} else {
final ViewGroup.MarginLayoutParams[] layoutParams = new ViewGroup.MarginLayoutParams[1];
layoutParams[0] = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
layoutParams[0].topMargin = extraTopWithoutCutout;
view.setLayoutParams(layoutParams[0]);
}
}
private int convertDpToPixel(float dp) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}
Надеюсь, это поможет вам !
Спасибо.