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

你是對的,我用協調器佈局重寫了我的活動佈局,現在一切都很好。謝謝:) –