9

używam poniższego układzie, CoordinatorLayout posiada w środku AppBarLayout (Z Toolbar i TabLayout w środku) i zastępczy RelativeLayout, więc mogę dodać i zastąpić fragmenty na nim.CoordinatorLayout z paska narzędzi oraz fragmentu

Występują błędy marginesu, fragmenty, które dodaję w RelativeLayout będą zawsze rozszerzały się poza dolną część ekranu (w wysokości podobnej do rozmiaru wysokości AppBarLayout), próbowałem ustawić jego wysokość na wrap_content i match_parent, w obu przypadkach wychodzi za burtę.

Jeśli usuniemy app:layout_behavior="@string/appbar_scrolling_view_behavior" z RelativeLayout, jego górna część będzie pod numerem AppBarLayout, co również nie jest pożądanym wynikiem.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer_layout" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:fitsSystemWindows="true"> 

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:layout_scrollFlags="scroll|enterAlways" /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       app:tabIndicatorHeight="4dp" 
       app:tabIndicatorColor="#ffffff" 
       app:tabMode="scrollable" 
       android:visibility="gone" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

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


     <RelativeLayout 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      android:id="@+id/main_fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 


     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end|bottom" 
      android:layout_margin="20dp" 
      android:src="@drawable/ic_done" /> 


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

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header" 
    app:menu="@menu/drawer_view"/> 

</android.support.v4.widget.DrawerLayout> 
+0

Czy możesz podać lepszy przykład? To, że 'RelativeLayout' nic nie robi i ma wysokość techniczną' 0' z powodu 'wrap_content', chyba że robisz coś z nim programowo. Jeśli zamierzasz użyć go jako "przechowującego", użyj 'FrameLayout'. –

+0

Umieszczam na nim fragment, wykonuję transakcję fragmentu i podając identyfikator RelativeLayout, próbowałem również użyć tego samego wyniku FrameLayout. – Calc

+0

Jest oczywiste, że masz to z przykładu Chrisa Banesa: https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml. Zauważ, że jego 'ViewPager' używa' match_parent'? –

Odpowiedz

0

Problem został rozwiązany przez aktualizację biblioteki recyclerview (do com.android.support:recyclerview-v7:22.2.0)

Fragment byłem ładowania miał recyclerview w nim.

2

Ten problem zostanie wyświetlony, jeśli w fragmencie znajduje się ScrollView. Więc upewnij się użyć NestedScrollView Zamiast:

<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
+0

Co z LinearLayout? Mam podobny problem z tym. – Calc

+0

Czy można zawinąć go w NestedScrollView? – bkurzius

3

I zorientowali się problem na fragment pokazano poniżej paska narzędzi przy użyciu układu koordynatora. Problem w moim przypadku jest:

In this image it shows the Fragment is overlapped by Toolbar.

Teraz wystarczy umieścić AppBarLayout i FrameLayout wewnątrz LinearLayout jak poniżej,

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/mainappbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways"/> 

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

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

Teraz problem został rozwiązany. I będzie tak.

The final image.

+0

'AppBarLayout' powinien być bezpośrednim potomkiem' CoordinatorLayout'. Zobacz: http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html – Orbit

1

Miał ten sam problem. Zmiana RelativeLayout na FrameLayout z parametrem app:layout_behavior="@string/appbar_scrolling_view_behavior" rozwiązała mój problem.

<FrameLayout 
    android:id="@+id/main_fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
Powiązane problemy