下面列出了android.view.View#setOnApplyWindowInsetsListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setStatusBarTranslucent(Window window, boolean translucent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = window.getDecorView();
if (translucent) {
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0,
defaultInsets.getSystemWindowInsetRight(),
defaultInsets.getSystemWindowInsetBottom());
}
});
} else {
decorView.setOnApplyWindowInsetsListener(null);
}
ViewCompat.requestApplyInsets(decorView);
}
}
/**
* 把状态栏设成透明
*/
protected void setStatusBarTransparent() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = getWindow().getDecorView();
decorView.setOnApplyWindowInsetsListener((v, insets) -> {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0,
defaultInsets.getSystemWindowInsetRight(),
defaultInsets.getSystemWindowInsetBottom());
});
ViewCompat.requestApplyInsets(decorView);
getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
}
/**
* Initialized to determine screen shape
*
* @param view
*/
private static void initShapeDetection(View view) {
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
shape = ScreenShape.ROUND;
if (screenWidthPX == 320 && screenHeightPX == 290) {
shape = ScreenShape.MOTO_ROUND;
}
} else {
shape = ScreenShape.RECTANGLE;
}
if (onShapeChangeListener != null) {
onShapeChangeListener.shapeDetected(getShape());
}
return insets;
}
});
}
private void applySystemWindowsBottomInset(int container) {
View containerView = findViewById(container);
containerView.setFitsSystemWindows(true);
containerView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
if (metrics.widthPixels < metrics.heightPixels) {
view.setPadding(0, 0, 0, windowInsets.getSystemWindowInsetBottom());
} else {
view.setPadding(0, 0, windowInsets.getSystemWindowInsetRight(), 0);
}
return windowInsets.consumeSystemWindowInsets();
}
});
}
/**
* Applies top window insets for a view.
*
* @param view The view to apply insets for.
*/
public static void applyTopWindowInsetsForView(@NonNull final View view) {
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
v.setPadding(v.getPaddingLeft(), insets.getSystemWindowInsetTop()
+ v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom());
return insets;
}
});
view.requestApplyInsets();
}
public static void setStatusBarTranslucent(Window window, boolean translucent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setRenderContentInShortEdgeCutoutAreas(window, translucent);
View decorView = window.getDecorView();
if (translucent) {
decorView.setOnApplyWindowInsetsListener((v, insets) -> {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0,
defaultInsets.getSystemWindowInsetRight(),
defaultInsets.getSystemWindowInsetBottom());
});
} else {
decorView.setOnApplyWindowInsetsListener(null);
}
ViewCompat.requestApplyInsets(decorView);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (translucent) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
ViewCompat.requestApplyInsets(window.getDecorView());
}
}
public static void getStatusBarHeight(final View view, final StatusBarHeightListener listener) {
if (STATUS_BAR_HEIGHT > 0) {
listener.onStatusBarHeightFetched(STATUS_BAR_HEIGHT);
}
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
STATUS_BAR_HEIGHT = insets.getSystemWindowInsetTop();
listener.onStatusBarHeightFetched(STATUS_BAR_HEIGHT);
return insets;
}
});
}
private void setupEdgeToEdgeLayout() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
decorView.setOnApplyWindowInsetsListener((v, insets) -> {
AppBarLayout.LayoutParams toolbarLayoutParams = new AppBarLayout.LayoutParams(mToolbar.getLayoutParams());
toolbarLayoutParams.setMargins(0, insets.getSystemWindowInsetTop(), 0, 0);
mToolbar.setLayoutParams(toolbarLayoutParams);
return insets;
});
}
private void setupEdgeToEdgeLayout() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
decorView.setOnApplyWindowInsetsListener((v, insets) -> {
CoordinatorLayout.LayoutParams mainFragmentLayoutParams = new CoordinatorLayout.LayoutParams(mMainFragment.getLayoutParams());
mainFragmentLayoutParams.setMargins(0, insets.getSystemWindowInsetTop(), 0, 0);
mMainFragment.setLayoutParams(mainFragmentLayoutParams);
return insets;
});
}
private void setupEdgeToEdgeLayout() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
decorView.setOnApplyWindowInsetsListener((v, insets) -> {
mToolbar.setPadding(0, insets.getSystemWindowInsetTop(), 0, 0);
return insets.consumeSystemWindowInsets();
});
}
/**
* 检测手表端设备屏幕形状
*
* @param view 用于检测的view
* @param callback 检测结果的回调
*/
@TargetApi(20)
public static void detectShapeOnWatch(View view, final ShapeDetectCallback callback) {
if (callback != null && shape != SHAPE_UNKNOWN) {
callback.onShapeDetected(shape);
return;
}
if (Build.VERSION.SDK_INT < 20) {
if (callback != null) {
callback.onShapeDetected(SHAPE_SQUARE);
}
return;
}
// Added in API level 20
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
shape = SHAPE_ROUND;
} else {
shape = SHAPE_SQUARE;
}
if (callback != null) {
callback.onShapeDetected(shape);
}
return insets;
}
});
}
public static void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayoutImpl) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
/**
* Initialized to determine screen shape
* @param view
*/
private static void initShapeDetection(View view){
if(!setOnApplyWindowInsetsListenerCalled) {
setOnApplyWindowInsetsListenerCalled = true;
view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
shape = ScreenShape.ROUND;
if (screenWidthPX == 320 && screenHeightPX == 290) {
shape = ScreenShape.MOTO_ROUND;
}
} else {
shape = ScreenShape.RECTANGLE;
}
if (onShapeChangeListeners != null && !onShapeChangeListeners.isEmpty()) {
synchronized (onShapeChangeListeners) {
for (OnShapeChangeListener listener : onShapeChangeListeners)
listener.shapeDetected(getShape());
}
}
return insets;
}
});
view.requestApplyInsets();
}
}
@Override
public void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayout) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
public static void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayoutImpl) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
public static void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayoutImpl) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
public static void configureApplyInsets(View drawerLayout) {
if (drawerLayout instanceof DrawerLayoutImpl) {
drawerLayout.setOnApplyWindowInsetsListener(new InsetsListener());
drawerLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}