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

我有一个片段活动.片段有一个协调器布局,fab作为它的直接子和其他视图.

活动布局

<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>

当我使用此片段开始活动时,fab位置不正确

《android – 启动时片段中的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.

解决方案:以下是AppBarLayout和CoordinatorLayout的文档,其中说明了它们的外观:

    <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的主布局中,然后在使用带有CoordinatorLayout的工具栏时尝试使用AppBarLayout因为现在CoordinatorLayout将RelativeLayout想象为它的AppBarLayout.

就是这样.还有一件事,不要忘记在appBarLayout下添加:app:layout_behavior =“@ string / appbar_scrolling_view_behavior”到你的视图.

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

点赞