2015-07-29 13 views
7

Tôi gặp sự cố khi sử dụng Thư viện hỗ trợ thiết kế Android. Tôi muốn sử dụng CoordinatorLayout bên trong đoạn cho FAB và ActionBar hình ảnh động trong khi di chuyển, nhưng tôi có ActionBar trong bố trí hoạt động và CardGridStaggeredView của tôi và FAB trong đoạn.Sử dụng CoordinatorLayout trong các đoạn với ActionBar trong Hoạt động

hoạt động bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/tools" 
    xmlns:app1="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

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

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

       <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_collapseMode="pin" 
        app:layout_scrollFlags="scroll|enterAlways" /> 

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

      <FrameLayout <!--Here I display fragments--> 
       android:id="@+id/fragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/header" 
     app:menu="@menu/drawer" 
     app1:itemTextColor="@android:color/black"/> 

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

đoạn bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       xmlns:card="http://schemas.android.com/apk/res-auto" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       style="@style/FragmentTheme" 
       tools:context="androidhive.info.materialdesign.activity.HomeFragment"> 

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

     <android.support.v4.widget.SwipeRefreshLayout 
      android:id="@+id/swipe_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <it.gmariotti.cardslib.library.extra.staggeredgrid.view.CardGridStaggeredView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:numColumns="2" 
       android:stretchMode="columnWidth" 
       android:verticalSpacing="2dp" 
       android:horizontalSpacing="2dp" 
       android:padding="1dp" 
       android:gravity="center" 
       card:list_card_layout_resourceID="@layout/carddemo_extras_base_staggered_card" 
       style="@style/main_layout_foreground" 
       android:id="@+id/cgvImages" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

     </android.support.v4.widget.SwipeRefreshLayout> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_add" 
      android:layout_gravity="bottom|end" 
      app:elevation="6dp" 
      app:pressedTranslationZ="12dp"/> 

    </FrameLayout> 
</RelativeLayout> 

Tôi cố gắng để sử dụng CoordinatorLayout trong hoạt động và Fragment, nhưng nó đã không làm việc. Có cách nào để sử dụng CoordinatorLayout với ActionBar trong Hoạt động và các chế độ xem khác trong phân đoạn không?

Trả lời

12

Tôi có Hoạt động với DrawerLayout và Những mảnh vỡ với CoordinatorLayout chỉ làm việc tốt (chú ý nơi fitsSystemWindows thuộc tính được thiết lập)

ở đây là bố trí hoạt động:

<?xml version="1.0" encoding="utf-8"?> 
<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_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <!-- Framelayout to display Fragments --> 
    <FrameLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:background="@color/color_primary" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/menu_container" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

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

      <!-- Listview to display slider menu --> 
      <ListView 
       android:id="@+id/list_slidermenu" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/bm_white" 
       android:choiceMode="singleChoice" 
       android:listSelector="@drawable/list_selector" 
       android:dividerHeight="0dp" 
       android:divider="@null" /> 
      <!-- Картинки - fb, vk, twitter --> 

     </LinearLayout> 

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

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

và xml đoạn với điều phối viên, nút hành động vv :

<?xml version="1.0" encoding="utf-8"?> 
<!--NOT SET HERE: 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:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- NOT SET HERE: android:fitsSystemWindows="true" --> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/detail_backdrop_height" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_collapseMode="parallax"> 

       <ImageView 
        android:id="@+id/backdrop" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_alignParentTop="true" 
        android:adjustViewBounds="true" 
        android:scaleType="centerCrop" 
        android:src="@drawable/pic_top_main" /> 

      </RelativeLayout> 

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

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

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

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     app:behavior_overlapTop="32dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:id="@+id/ll_main_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <!-- In code now… --> 
      <!--<include layout="@layout/fragment_start_nonfc"/>--> 
      <!--<include layout="@layout/fragment_start"/>--> 

     </LinearLayout> 

    </android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_margin="@dimen/fab_margin" 
     android:clickable="true" 
     android:onClick="onClickPurchase_Remote" 
     android:src="@drawable/btn_purchase" 
     app:borderWidth="0dp" 
     app:elevation="6dp" 
     app:fabSize="normal" 
     app:pressedTranslationZ="12dp" /> 

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

Cảm ơn bạn đã trả lời. Bây giờ nó hoạt động, nhưng CardGridStaggeredView của tôi không hiển thị trong NestedScrollView. Nếu tôi xóa Thẻ NestedScrollView thì hiển thị, nhưng sau đó hoạt ảnh không hoạt động. –

+0

Đúng vậy ... không phải tất cả các kết hợp đều hoạt động với các điều khiển hiện tại. Cố gắng đưa các điều khiển của bạn bên trong android.support.v7.widget.CardView Vì vậy, hệ thống cấp bậc đó là

+0

Tôi đang đấu tranh với điều này là tốt. Tôi đang cố gắng để có được bối cảnh được vẽ phía sau thanh trạng thái, nhưng nó vẫn được vẽ bên dưới bằng cách sử dụng mã của bạn. Còn thiếu gì nữa không? Tôi có các cờ 'fitsSystemWindows' được đặt chính xác như bạn có. Có bất kỳ mã nào cần phải đi trong các mảnh? – Pin

Các vấn đề liên quan