Use ViewCompat to get window insets on Android 5.0+.
On devices running API 20 and below, getRootWindowInsets() always returns null.
This commit is contained in:
parent
7451ee1403
commit
a98cc5706f
1 changed files with 22 additions and 10 deletions
|
@ -39,6 +39,8 @@ import androidx.annotation.NonNull;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
|
||||
|
@ -326,21 +328,31 @@ public final class ViewUtil {
|
|||
}
|
||||
|
||||
public static int getStatusBarHeight(@NonNull View view) {
|
||||
int result = 0;
|
||||
int resourceId = view.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = view.getResources().getDimensionPixelSize(resourceId);
|
||||
final WindowInsetsCompat rootWindowInsets = ViewCompat.getRootWindowInsets(view);
|
||||
if (rootWindowInsets != null) {
|
||||
return rootWindowInsets.getInsets(WindowInsetsCompat.Type.statusBars()).top;
|
||||
} else {
|
||||
int result = 0;
|
||||
int resourceId = view.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = view.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getNavigationBarHeight(@NonNull View view) {
|
||||
int result = 0;
|
||||
int resourceId = view.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = view.getResources().getDimensionPixelSize(resourceId);
|
||||
final WindowInsetsCompat rootWindowInsets = ViewCompat.getRootWindowInsets(view);
|
||||
if (rootWindowInsets != null) {
|
||||
return rootWindowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom;
|
||||
} else {
|
||||
int result = 0;
|
||||
int resourceId = view.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = view.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void hideKeyboard(@NonNull Context context, @NonNull View view) {
|
||||
|
|
Loading…
Add table
Reference in a new issue