2017-02-06 51 views
0

Iam嘗試在我的項目中爲片段和活動創建摺疊工具欄,但摺疊工具欄無法正常工作。請幫助我找到解決方案,正如iam new to android。 預先感謝您。如何在片段和活動中創建摺疊工具欄

+0

你試過這個解決方案嗎? http://stackoverflow.com/questions/30739806/coordinator-layout-with-toolbar-in-fragments-or-activity – AndroidBeginner

回答

0

首先,您需要創建一個AppBar佈局作爲主要父級。在這裏面添加一個CollapsingToolbarLayout。在CollapsingToolbarLayout裏面添加你想要的東西,在你的工具欄中,最後添加工具欄。

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

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsing_toolbar_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:contentScrim="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways"> 

<!--This can be anything, here I've added a view pager inside collapsing toolbar--> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/pager_introduction" 
        android:layout_width="match_parent" 
        android:layout_height="192dp" 
        android:fitsSystemWindows="true" 
        app:layout_collapseMode="parallax" 
        tools:listitem="@layout/pager_item" /> 

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

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

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

現在爲android部分實例化工具欄並進行設置。

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

在我來說,我已經添加了viewpager,一個單獨的適配器將不得不創建來填充viewpager。

相關問題