嵌套的ScrollView在顶部留下了一些额外的空间

3

我在AppBarLayout下有一个NestedScrollView,它只有一个子控件-LinearLayout。它们都没有顶部填充和边距,但是在NestedScrollView的顶部仍然留有一些空间(根据背景颜色可以看出)。如何去除它?

我尝试在NestedScrollView中设置android:fillViewport="true",但没有效果。

更新:这个空间与clipNoPadding标志交互,但将所有填充设置为0也无济于事。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:background="@color/colorAccent"
        android:fillViewport="true"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:orientation="vertical">

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

            // and some more buttons, just to fill the space

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        android:background="@color/colorTransparent">

        // I omit some code, as not-belonging to the question

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

它的效果展示


1
如果您删除fitsSystemWindows属性会发生什么? - Ben P.
@BenP。哦,太好了,它有帮助!如果你想的话,你可以把它发布为一个答案。 - pseusys
1个回答

5
android:fitsSystemWindows="true"属性会为应用该属性的视图增加一些(通常是24dp)的顶部内边距。这意味着它应该与透明或半透明状态栏(或导航栏)一起使用,以便您的视图内容不会出现在状态栏下方。
从您的截图中看不出您是否正在使用透明状态栏,因此这只会添加您不需要的24dp内边距。请将其删除。
还要注意的是,android:fitsSystemWindows将覆盖使用该属性的视图上的任何其他内边距。因此,即使您手动将内边距设置为0,仍然会得到24dp的内边距。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接