CoordinatorLayout + ActionBar + Fragments

Ada*_*uez 5 android android-fragments android-coordinatorlayout android-support-design android-appbarlayout

当我尝试将片段加上CoordinatorLayout+ 时,我遇到了问题AppBarLayout.

我尝试将不同的片段加载到具有属性的ActionBar下面的RelativeLayout内容中,app:layout_behavior="@string/appbar_scrolling_view_behavior"但是当我在屏幕底部加载带有两个按钮的片段时,这些片段被加载到屏幕外.

加载片段的内容在屏幕外,内容总是从底部开始.

这是我的代码main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay"  >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

    <!-- Fragment are loaded here -->
    <RelativeLayout
        android:id="@+id/containerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这是屏幕内容的屏幕截图.

在此输入图像描述

这是我遇到问题的片段截图: 在此输入图像描述

这是在模拟器中加载片段的屏幕截图.您可以看到底部按钮的显示方式.它们被导航栏隐藏:

在此输入图像描述

我该如何防止这个问题?

编辑

<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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay"  >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

    <!-- Main content -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
           android:id="@+id/containerLayout"
           android:layout_width="match_parent"
           android:layout_height="match_parent">

        </FrameLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

编辑2:我尝试将ToolBar放入RelativeLayout,但它部分工作.滚动到recyclerView时,现在不能在actionBar上运行动画

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".views.activities.HomeActivity">

    <!-- Main content -->
    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" > 

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:layout_alignParentTop="true">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->

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

        <FrameLayout
            android:id="@+id/containerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appBarLayout">

        </FrameLayout>

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

kri*_*son 2

我最终发现,发生的情况CoordinatorLayout是,下面的视图的AppBarLayout大小是根据高度调整的,就好像Toolbar已经滚动到一边一样。然后,当工具栏处于正常位置时,只需将视图向下推即可。当所有内容向上滚动时,它不会调整大小CoordinatorLayout

这意味着附加到该视图底部的任何视图都将滚动到一边,并且不可见或部分可见。

那么如何解决这个问题呢?你需要做两件事:

  1. AppBarLayoutToolbar放入您的片段中。该片段可以设置ActionBar对 的支持Toolbar并执行通常在活动中执行的所有操作。所以现在你的活动布局可以像这样简单:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/containerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".views.activities.HomeActivity"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将按钮附加到 的底部CoordinatorLayoutCoordinatorLayout是 的子类,FrameLayout因此您可以layout_gravity="bottom"在子视图上使用。现在您的片段 XML 可能如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".views.activities.HomeActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay"  >
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_scrollFlags="scroll|enterAlways" /> <!-- Indica como afecta a los hijos de AppBar -->
    
        </android.support.design.widget.AppBarLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    
            <!-- 
              rest of your fragment layout goes here 
              it needs to have a scrolling component
              -->
    
            <!-- 
              you *might* need a spacer in order to see
              the bottom of your view over the top of
              the buttons
              -->
            <Space
                android:layout_width="match_parent"
                android:layout_height="48dp" />
            <!-- or you could put a bottom margin on your layout -->
    
        </RelativeLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom">
    
            <!-- your buttons go here -->
    
        </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    Run Code Online (Sandbox Code Playgroud)