0

我正在閱讀11章的「大書呆子牧場指南」,我正在撰寫與Material Design非常相似的應用。應用程序有兩項活動 - 列表和項目從此列表中刪除。在detalization活性實施ViewPageronCreate方法與此代碼:如何使用ViewPager製作可見的工具欄?

mViewPager = new ViewPager(this); 
mViewPager.setId(R.id.viewPager); 
setContentView(mViewPager); 

setContentView如何理解我們不使用XML標記,所以我失去了我的Toolbar

scr1 scr2

在這本書中應用的下一個章節都有ActionBar。我怎樣才能將我的Toolbar歸還給違約活動?

book scr

+0

你應該把ViewPager在你的XML佈局,在這裏看到:http://stackoverflow.com/questions/31698756/remove-line-break-in- tablayout/32547335#32547335底部的選項卡:http://stackoverflow.com/questions/32984706/how-can-i-set-tabs-at-the-bottom-and-also-hide-top-actionbar –

+0

@ DanielNugent我有特別複雜的體系結構和本書的片段,我正在嘗試升級這個應用程序,但我不太清楚它是如何工作的,所以我不明白爲什麼我在工具欄,FAB和導航抽屜中丟失了第二項活動。如果你有空閒時間,你可以看看我的存儲庫,並給我更多的conrete建議? https://github.com/kostyabak/TeamMaster – Kostya

+1

你失去了TabLayout和FAB,因爲你只用ViewPager調用'setContentView()'。如果你想有一個ViewPager和一個TabLayout和一個FloatingActionButton,只需在一個包含全部三個XML佈局的XML佈局中調用'setContentView()'。查看關於實現細節的鏈接答案。 –

回答

2

你,因爲你打電話setContentView()只用ViewPager失去了TabLayout和FAB。

如果你想有一個ViewPager和一個TabLayout和一個FloatingActionButton,只需在三個XML佈局中調用setContentView()。像這樣將使TabLayout頂部:

<RelativeLayout 
    android:id="@+id/main_layout" 
    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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     app:tabMode="fixed" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     app:tabTextColor="#d3d3d3" 
     app:tabSelectedTextColor="#ffffff" 
     app:tabIndicatorColor="#ff00ff" 
     android:minHeight="?attr/actionBarSize" 
     /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/tab_layout"/> 

</RelativeLayout>