5

以下是代碼。從摺疊工具欄中不顯示工具欄

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

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/mytoolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:title="@string/app_name" 
       app:layout_collapseMode="pin" 
       app:theme="@style/ThemeOverlay.AppCompat.Light"/> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/main_tablayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       app:layout_collapseMode="none"/> 


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

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

    <!--<android.support.v4.widget.NestedScrollView--> 
     <!--android:id="@+id/nestedscroll"--> 
     <!--android:layout_width="match_parent"--> 
     <!--android:layout_height="match_parent"--> 
     <!--android:fillViewport="true"--> 
     <!--android:scrollbars="horizontal"--> 
     <!--app:layout_behavior="@string/appbar_scrolling_view_behavior">--> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/main_viewpager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/main_fab" 
     android:layout_margin="16dp" 
     android:src="@android:drawable/ic_media_play" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_anchor="@id/main_viewpager" 
     app:layout_anchorGravity="bottom|end"/> 

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

問題:
1)工具欄是不可見的。
2)摺疊工具欄根本不會摺疊。 [解決]
3)如果放入nestedScrollView中,Viewpager和FAB也不可見。 [解決]

額外的細節:
佈局Viewpager的片段具有的LinearLayout如根和內部具有一個recyclerview。

根據代碼,一切似乎都沒問題。無法理解缺失的內容。協調器佈局和摺疊工具欄如何協同工作的一個很好的解釋也確實有幫助。

回答

4

1)工具欄不可見。

所有你需要定義你想在你的活動課使用什麼工具欄的第一:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

更改現有的XML代碼:

 <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      app:title="@string/app_name" 
      app:layout_collapseMode="parallax"> 
     </android.support.v7.widget.Toolbar> 

到:

 <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" //set initial height 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" //this might be also useful 
      app:title="@string/app_name" 
      app:layout_collapseMode="parallax" /> 

2)摺疊工具欄根本不會摺疊。

使用正確的主題做了您的活動。設置爲您AppBarLayout

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 

如下例:include_list_viewpager.xml

3)Viewpager和FAB此外,如果把裏面的nestedScrollView不可見。

沒有理由這樣做。添加這些行:

android:layout_marginTop="?attr/actionBarSize" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 

ViewPager應該足夠了。

他們倆都應該是CoordinatorLayout的直接子女。

按照這個例子:http://blog.nkdroidsolutions.com/collapsing-toolbar-with-tabs-android-example/

如果你是新材料的設計或覺得有點有一些自己的行爲失去了,我強烈建議檢查克里斯巴內斯材料設計項目cheesequarehttps://github.com/chrisbanes/cheesesquare/

希望它將有助於

+0

已經算了第二和第三。它的第一個不工作,即使這樣做後「工具欄工具欄=(工具欄)findViewById(R.id.toolbar); setSupportActionBar(toolbar);' –

+0

@DarshanMiskin我更新了我的答案。您忘記設置默認的工具欄高度 - 在這種情況下'wrap_content'可能等於零。屬性'actionBarSize'設置Android標準工具欄大小。 – piotrek1543

+0

嘗試過..仍然沒有.. –

2

首先你應該告訴你正在使用的工具欄您的活動,所以在onCreate方法,你應該有:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

你的第二個和第三個問題要解決一起。您應該使用NestedScrollView作爲ViewPager中片段的主要佈局,然後在裏面放置您的LinearLayout或其他任何東西。

+0

NestedScrollView不是必需的屬性'layout_behavior'很重要。檢查了這一點http://stackoverflow.com/questions/31275277/viewpager-not-appearing-when-using-collapsingtoolbarlayout。另外,更改主題後,摺疊工具欄不會摺疊。現在將檢查您的代碼。 –

+0

仍然無法正常工作。 –

+0

更改主題後,摺疊工具欄爲** NOW **摺疊。 –