Android-fitsSystemWindows属性存在问题
我需要你的帮助来理解一种奇怪的行为。当我将fitsSystemWindows属性设置为“true”时,导航栏会隐藏布局的某些部分,请参见下图: 当我设置为false时,我有以下行为(可以): 当我阅读Android文档和许多关于Stackoverflow的帖子时,我理解它应该与这种行为完全相反:。 fitsSystemWindows='true'的第一个案例应该可以,第二个案例应该被导航栏隐藏,我错了吗 有人能解释一下发生了什么事吗?我的targetVersionSdk是29,我在很多版本(Android 6、7、10和11)上测试了它。也许这是特定于协调布局的?谢谢你的解释:) 以下是我的xml布局:Android-fitsSystemWindows属性存在问题,android,android-layout,layout,Android,Android Layout,Layout,我需要你的帮助来理解一种奇怪的行为。当我将fitsSystemWindows属性设置为“true”时,导航栏会隐藏布局的某些部分,请参见下图: 当我设置为false时,我有以下行为(可以): 当我阅读Android文档和许多关于Stackoverflow的帖子时,我理解它应该与这种行为完全相反:。 fitsSystemWindows='true'的第一个案例应该可以,第二个案例应该被导航栏隐藏,我错了吗 有人能解释一下发生了什么事吗?我的targetVersionSdk是29,我在很多版本(
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="[true or false]">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
[...]
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_vertical_margin"
android:src="@drawable/ic_arrow_forward_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
[...]
我认为视图工作正常。首先,您需要了解插图及其传递方式。fitsSystemWindow
的默认行为是使用所有插入并将其作为填充应用。但CoordinatorLayout、DrawerLayout等视图组会覆盖此行为
private void setupForInsets() {
if (Build.VERSION.SDK_INT < 21) {
return;
}
if (ViewCompat.getFitsSystemWindows(this)) {
if (mApplyWindowInsetsListener == null) {
mApplyWindowInsetsListener =
new androidx.core.view.OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v,
WindowInsetsCompat insets) {
return setWindowInsets(insets);
}
};
}
ViewCompat.setOnApplyWindowInsetsListener(this, mApplyWindowInsetsListener);
setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
}
下面是覆盖行为的CoordinatorLayout中的代码片段
private void setupForInsets() {
if (Build.VERSION.SDK_INT < 21) {
return;
}
if (ViewCompat.getFitsSystemWindows(this)) {
if (mApplyWindowInsetsListener == null) {
mApplyWindowInsetsListener =
new androidx.core.view.OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v,
WindowInsetsCompat insets) {
return setWindowInsets(insets);
}
};
}
ViewCompat.setOnApplyWindowInsetsListener(this, mApplyWindowInsetsListener);
setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
}
你可以在博客文章中了解更多有关插图的信息。你的窗口上设置了
标志\布局\无限制
或系统\ UI \标志\隐藏\导航
吗?嘿,Pawel,不,我没有!我为我迟来的回答向苏珊道歉!非常感谢您的精彩解释:)