我已经用viewPager为片段事务创建了tablayout活动。
现在我的设计改变了,要增加抽屉。因为汉堡菜单不起作用。当从左边滑动时,它会出现,而当我按下图标时,它不会显示。我认为可能是布局问题。但我正在努力修改布局。
我试过了,但是,如果我改变抽屉的位置,整个碎片都会向上移动。
布局:
<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">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/olive"
app:tabMode="scrollable"
app:tabGravity="fill"/>
</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.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout">
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/shitstuff"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:layout_marginTop="-24dp"/>
</android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>
选项卡布局的单独文件用于附加其中的所有选项卡。
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="helix.profitkey.hotelapp.SimpleTabsActivity">
<include layout="@layout/status_bar"/>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="helix.navigationdrawer.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_selected_color"
app:tabTextColor="@color/silver"
app:tabSelectedTextColor="@color/white"
app:tabMode="scrollable"
app:tabGravity="fill"
/>
</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>
</RelativeLayout>
然后xml被附加到抽屉布局中。在抽屉里,我使用导航视图添加了选项或操作的列表。导航视图应用程序:menu="@menu/drawermenu"这将添加drawerenu中的项目列表。
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/violet_light"
android:layout_gravity="start"
android:id="@+id/drawerstuff"
app:itemTextColor="@color/violet_statusbar"
app:menu="@menu/drawermenu"
android:theme="@style/NavigationTheme"
android:layout_marginTop="-24dp"/>
</android.support.v4.widget.DrawerLayout>
@menu/drawermenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Title"
android:id="@+id/id"
android:icon="@drawable/tyre"/>
<item android:title="Title"
android:id="@+id/id1"
android:icon="@drawable/tyre"/>
<item android:title="Title"
android:id="@+id/id2"
android:icon="@drawable/tyre"/>
<item android:title="Title">
<menu>
<item
android:title="Title"
android:id="@+id/id3"
android:icon="@drawable/tyre"/>
<item
android:title="Title"
android:id="@+id/id4"
android:icon="@drawable/tyre"/>
</menu>
</item>
<group android:id="@+id/group_settings_id">
<item android:title="Settings"
android:id="@+id/nav_item_settings"
android:icon="@drawable/tyre"/>
<item android:title="About"
android:id="@+id/nav_item_about"
android:icon="@drawable/tyre"/>
<item android:title="Help"
android:id="@+id/nav_item_write_help"
android:icon="@drawable/tyre"/>
<item android:title="LogOut"
android:id="@+id/nav_item_write_logout"
android:icon="@drawable/tyre"/>
</group>
</menu>