需要包含带有导航抽屉的标签

时间:2016-06-14 02:00:14

标签: android

我的主要活动布局现在看起来像这样:

<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

也就是说,它有碎片和导航抽屉。唯一遗漏的是实际标签。

3 个答案:

答案 0 :(得分: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 :(得分:1)

答案 2 :(得分: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 -->
相关问题