2016-06-14 95 views
1

我的主要活動佈局選項卡uptil現在看起來是這樣的:必須包括導航抽屜

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

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

      <include 
       android:id="@+id/toolbar" 
       layout="@layout/toolbar" > 
      </include> 

     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 
     </FrameLayout> 

     <android.support.design.widget.CoordinatorLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

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

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


    </LinearLayout> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="android.mybitchinapp.com.testgui.activity.FragmentDrawer" 
     android:layout_width="@dimen/nav_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" > 
    </fragment> 

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

我需要包括TabLayout以提供選項卡的應用程序。我不知道如何包含它。我試圖添加一個AppBarLayout(它將包含TabLayout),但它導致應用程序崩潰。所以它顯然不是一個解決方案。我如何編輯它以使其具有TabLayout? 需要的最終產品是: enter image description here

礦看起來像這樣: enter image description here

也就是說,它有碎片和Tha導航抽屜。唯一缺少的是實際的選項卡。

+0

https://developer.android.com/reference/android/support/design/widget /TabLayout.html – Karakuri

+0

我已經明白我需要一個TabLayout。我只想知道我應該把它放在我的佈局中。直接將它放入DrawerLayout會導致應用程序崩潰。 –

回答

3

XML佈局顯得撲朔迷離。所以我編輯了你的xml作爲一個基本的想法來實現你所需要的作爲你給定的截圖。

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <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:fitsSystemWindows="true"> 

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

       <include 
        android:id="@+id/toolbar" 
        layout="@layout/toolbar" > 
       </include> 

       <android.support.design.widget.TabLayout 
        android:id="@+id/tabs" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

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

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

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

      <fragment 
       android:id="@+id/fragment_navigation_drawer"   
       android:layout_width="@dimen/nav_drawer_width" 
       android:layout_height="match_parent" 
       android:layout_gravity="start" 
       app:layout="@layout/fragment_navigation_drawer" 
       tools:layout="@layout/fragment_navigation_drawer" > 
      </fragment> 

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

參考:http://hmkcode.com/material-design-app-android-design-support-library-appcompat/

+1

感謝您的來源。 –

0

在您當前的佈局中,您可以在包含工具欄後添加TabLayout

<!-- snip --> 
<LinearLayout 
    android:id="@+id/container_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:layout_height="some_size" /> 

</LinearLayout> 
<!-- snip --> 
+0

這導致應用程序在安裝時崩潰:/ –

+0

@leehuang在你的帖子中包含你在_code_和堆棧跟蹤中所做的事情會對你有所幫助。我們不是通靈者:/ – Karakuri