4

我有一个片段活动。片段有一个协调器布局,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

但是当我按任何按钮或视图晶圆厂在r的正确位置ight底角。当我把这个布局直接放到活动晶圆厂的位置上时,总是正确的。有谁能帮我弄清楚有什么问题吗?

回答

1

但是当我按任何按钮或视图工厂去在右下角其正确位置 。 当我把这个布局直接放入 活动晶圆厂的位置总是正确的。

解决方案:这里是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因为现在CoordinatorLayout想象的RelativeLayout,因为它是AppBarLayout使用AppBarLayout

就是这样。还有一件事,别忘了在AppBarLayout的基础上加上:app:layout_behavior="@string/appbar_scrolling_view_behavior"

让我知道你是否需要任何帮助或提问。

+0

你好,谢谢你的回答,但是我在问题描述中做了一个错误。当我**将fragment的布局直接放入fragment_container位置的activity **时,一切都很好,但是当**我将fragment的布局用作activity布局**时。我需要这个RelativeLayout来将RecyclerView放置在Expandable布局下。 –

+0

为什么不能在** Activity Layout **中使用'CoordinatorLayout'然后将FloatingActionButton放在正确的位置?我只是在想,为什么你把'Toolbar'放在'LinearLayout'里面,然后把'CoordinatorLayout'放在没有'Toolbar'和'AppBarLayout'的片段上!我不认为如果这种布局设计是正确的! – Mohsen

+1

你是对的,我用协调器布局重写了我的活动布局,现在一切都很好。谢谢:) –