启动时片段中的 fab 位置不正确

Incorrect fab position in fragment on startup

我有一个片段activity。 fragment 有一个协调器布局,可以作为它和其他视图的直接子级。

Activity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_layout" />

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

</LinearLayout>

片段布局

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.aakira.expandablelayout.ExpandableLinearLayout
        android:id="@+id/statistic_expand_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        app:ael_duration="150"
        app:ael_interpolator="accelerateDecelerate">

        <com.github.mikephil.charting.charts.BarChart
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/overall_statistic_bar_chart"
            android:layout_width="match_parent"
            android:layout_height="@dimen/statistic_small"/>

        <TextView
            style="@style/SmallTextTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/spacing_small"
            android:gravity="center"
            android:text="@string/overall_statistic"
            android:textColor="@color/colorTextPrimary"/>

    </com.github.aakira.expandablelayout.ExpandableLinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/statistic_expand_layout"
        android:scrollbars="vertical"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_tiny"
        android:layout_below="@id/statistic_expand_layout"
        android:background="@drawable/shadow_updown"/>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/activity_main_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/spacing_standard"
    android:src="@drawable/ic_add"
    app:layout_anchor="@id/recycler_view"
    app:layout_anchorGravity="bottom|end"/>

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

当我开始activity这个片段fab位置不正确

但是当我按下任何按钮或查看 fab 时,它会转到右下角的正确位置。当我将此布局直接放入 activity 时,fab 位置总是正确的。谁能帮我找出问题所在?

but when I press any button or view fab goes to its correct position in the right bottom corner. When I put this layout directly into activity fab position is always correct.

解决方案: 这是 AppBarLayoutCoordinatorLayout 的文档,其中说明了它们的外观:

    <android.support.design.widget.CoordinatorLayout
         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.support.v4.widget.NestedScrollView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <!-- Your scrolling content -->

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

     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

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

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

所以现在,RelativeLayout 在您的设计中不应该存在。然后把CoordinatorLayout放在Activity的main layout里面,使用的时候尽量用AppBarLayout ToolbarCoordinatorLayout 因为现在 CoordinatorLayout 想象 RelativeLayoutAppBarLayout

就是这样。还有一件事,不要忘记在 AppBarLayout.

下的视图中添加:app:layout_behavior="@string/appbar_scrolling_view_behavior"

如果您需要任何帮助或提出问题,请告诉我。