Tôi sẽ thêm điều này vào đây trong trường hợp bất kỳ ai cần xóa phần đệm trên cùng khi sử dụng fitSystemWindows. Đây có thể là trường hợp khi sử dụng thanh tác vụ tùy chỉnh, DrawerLayout/NavigationView và/hoặc đoạn.
public class CustomFrameLayout extends FrameLayout {
public CustomFrameLayout(Context context) {
super(context);
}
public CustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected boolean fitSystemWindows(Rect insets) {
// this is added so we can "consume" the padding which is added because
// `android:fitsSystemWindows="true"` was added to the XML tag of View.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < 20) {
insets.top = 0;
// remove height of NavBar so that it does add padding at bottom.
insets.bottom -= heightOfNavigationBar;
}
return super.fitSystemWindows(insets);
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
// executed by API >= 20.
// removes the empty padding at the bottom which equals that of the height of NavBar.
setPadding(0, 0, 0, insets.getSystemWindowInsetBottom() - heightOfNavigationBar);
return insets.consumeSystemWindowInsets();
}
}
Chúng ta phải mở rộng các lớp Layout (FrameLayout trong trường hợp của tôi) và loại bỏ các đệm hàng đầu trong fitSystemWindows()
(đối với API < 20) hoặc onApplyWindowInsets()
(đối với API> = 20).
Điều này đã được giải quyết trong bài viết - [Tại sao tôi muốn phù hợpSystemWindows?] (Https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.kpokdt33j). – Sufian