【问题标题】:Persistent BottomSheet using NestedScrollView, it contains top padding? support-lib 24.2.1使用 NestedScrollView 的 Persistent BottomSheet,它包含顶部填充?支持库 24.2.1
【发布时间】:2016-12-16 09:48:19
【问题描述】:

有很多很好的教程使用NestedScrollView 讨论BottomSheet。但是在我使用当前的支持库版本 24.2.1 进行尝试后,我花了几天的时间才知道为什么我的包含顶部填充。

这是我的 Activity 的布局 xml:

<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"
    tools:context=".ui.MainActivity">

    <!-- AppBar -->
    <include
        android:id="@+id/appBar"
        layout="@layout/app_bar_layout_with_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabPager="@{tabPager}" />

    <!-- Content -->
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <!-- [START] BottomSheet -->
    <include
        android:id="@+id/bottomSheetMain"
        layout="@layout/fragment_bottom_sheet_song"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:behavior_hideable="false"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="@string/bottom_sheet_behavior" />
    <!-- [END BottomSheet] -->

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

还有BottomSheet的布局fragment_bottom_sheet_song.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bgBottomSheetPlayer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/bottomSheetPlayer"
            layout="@layout/view_bottom_sheet_player_dark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:playAction="@{playAction}"
            app:playObject="@{playSong}" />

        <include
            android:id="@+id/bottomSheetContent"
            layout="@layout/view_action_detail_song"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:playSong="@{playSong}"
            app:songActionHandler="@{songActionHandler}" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

那么结果如下:

为什么会有顶部填充?我该如何解决这个问题?

谢谢。

【问题讨论】:

    标签: android android-support-library android-support-design bottom-sheet


    【解决方案1】:

    经过几天的尝试,我发现要解决这个问题,持久的底部表单布局容器必须是FrameLayout。如果不是(就像我的情况,我直接包含了NestedScrollView 并使其成为底部表),它将包含默认的顶部填充,我仍然不知道为什么。

    所以这是我要修复的新 Activity 的 xml 布局代码:

    <!-- [START] BottomSheet -->
    <FrameLayout
        android:id="@+id/bottomSheetParent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:behavior_hideable="false"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="@string/bottom_sheet_behavior">
    
        <include
            android:id="@+id/bottomSheetMain"
            layout="@layout/fragment_bottom_sheet_song"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>
    <!-- [END BottomSheet] -->
    

    希望这对你也有帮助。

    【讨论】:

    • @maoheing 这不可能,在我的情况下它也适用于线性布局。
    • 对于那些面临这个问题的人,在他的示例中观察到的顶部填充是窗口系统插图,它根据其层次结构自动添加到特定视图。在这种情况下,顶部填充与状态栏的高度相同,一旦工作表完全打开,它又将用于帮助显示其下方而不是其后面的内容。
    【解决方案2】:

    尝试将其添加到您的 Activity 清单中,显然在我的情况下是键盘导致底部表自动提供填充。

    android:windowSoftInputMode="adjustPan" 
    

    【讨论】:

      【解决方案3】:

      我认为您需要从该视图的根布局 android.support.design.widget.CoordinatorLayout 中删除标签 android:fitsSystemWindows="true"。您的源代码布局将是这样的。

      <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"
          tools:context=".ui.MainActivity">
      
      <!-- children contents -->
      
      </android.support.design.widget.CoordinatorLayout>
      

      希望对您有所帮助。

      【讨论】:

      • 现在发生在我身上,删除提到的标签解决了这个问题。
      猜你喜欢
      • 2017-01-25
      • 2017-01-28
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多