【问题标题】:Incorrect fab position in fragment on startup启动时片段中的晶圆厂位置不正确
【发布时间】:2017-05-13 19:21:30
【问题描述】:

我有一个片段活动。 fragment 有一个协调器布局,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 位置不正确

但是当我按下任何按钮或查看 fab 时,它会转到右下角的正确位置。当我将此布局直接放入活动工厂时,位置总是正确的。谁能帮我弄清楚是什么问题?

【问题讨论】:

    标签: android android-coordinatorlayout floating-action-button


    【解决方案1】:

    但是当我按下任何按钮或查看 fab 时,它会转到正确的位置 在右下角当我把这个布局直接放入 活动 晶圆厂位置始终正确。

    解决方案:这是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 的主布局中 然后,在使用ToolbarCoordinatorLayout 时尽量使用AppBarLayout 因为现在CoordinatorLayoutRelativeLayout 想象成AppBarLayout

    就是这样。还有一件事,别忘了在AppBarLayout 下的视图中添加:app:layout_behavior="@string/appbar_scrolling_view_behavior"

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

    【讨论】:

    • 您好,谢谢您的回答,但我在问题描述中写错了。当我将片段的布局直接放入片段容器位置上的活动时,一切都很好,但是当我将片段的布局用作活动布局时。我需要这个 RelativeLayout 来在展开时将 RecyclerView 定位在 Expandable 布局下。
    • 为什么不能在 Activity 布局 中使用CoordinatorLayout,然后将FloatingActionButton 放在正确的位置?我只是在想,为什么你把Toolbar 放在LinearLayout 里面然后对于你使用的片段CoordinatorLayout 没有ToolbarAppBarLayout !!我不认为这样的布局设计是否正确!
    • 你说得对,我用协调器布局重写了我的活动布局,现在一切都很好。谢谢你:)
    • @OlexiiMuraviov 太棒了!很高兴听到这个消息!
    猜你喜欢
    • 2019-03-05
    • 2017-02-10
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多