【问题标题】:Add DrawerLayout to TabLayout with ViewPager使用 ViewPager 将 DrawerLayout 添加到 TabLayout
【发布时间】:2017-08-23 14:15:11
【问题描述】:

我目前有一个使用 TabLayout 的 ViewPager。我根据 ViewPager 的页面数动态地将元素添加到 TabLayout。

我想在 TabLayout 的左侧添加一个带有汉堡图标的 DrawerLayout。滚动 TabLayout 时,此汉堡图标应保持可见。

我尝试使用父级 RelativeLayout 在 TabLayout 旁边添加 DrawerLayout,但没有成功。

这是我想做的:

这是当前的 XML:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v4.widget.DrawerLayout
                android:id="@+id/drawer_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <!-- Side navigation drawer UI -->
                <ListView
                    android:id="@+id/navList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="left|start"
                    android:background="#ffeeeeee"/>

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/drawer_layout"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
            </RelativeLayout>

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

    ...
    ...
    ...

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

【问题讨论】:

    标签: android android-viewpager android-tablayout drawerlayout navigation-drawer


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/lib/com.app.chasebank"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:minHeight="?attr/actionBarSize"
          android:background="#ef453e"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
        <android.support.design.widget.TabLayout
          android:id="@+id/tabLayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#ef453e"
          android:minHeight="?attr/actionBarSize"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:tabIndicatorColor="@android:color/white"
          app:tabIndicatorHeight="5dp" />
    
        <android.support.v4.widget.DrawerLayout
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
            <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
              <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"/>
            </RelativeLayout>
            <ListView
              android:id="@+id/listDrawer"
              android:layout_width="240dp"
              android:layout_height="match_parent"
              android:layout_gravity="right"
              android:choiceMode="singleChoice"
              android:background="#FF0000"/>
        </android.support.v4.widget.DrawerLayout>
    
    </LinearLayout>
    

    我也在做同样的任务,这就是我所做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2018-04-25
      相关资源
      最近更新 更多