Android 在单个 activity 中有一个导航抽屉和工具栏
Android have a navigation drawer and toolbar in a single activity
我正在开发一个需要 ActionBar 选项卡和导航抽屉的应用程序。然而,这两者似乎在布局上相互冲突。对于导航抽屉,我需要有一个 DrawerLayout
作为根布局,然后是抽屉和内容的另外两个视图。这真的很容易,而且效果很好。执行此操作的代码是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/navdrawer"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我可以说它甚至不需要 运行 或任何其他工作就可以工作:
问题是我需要 TabLayout
、ViewPager
和自定义 ToolBar
,所有这些都需要 CoordinatorLayout
作为根元素才能正常工作。无论如何我可以获得正常工作所需的三个视图,将 DrawerLayout
保持为根?我试过在没有 CoordinatorLayout
作为根视图的情况下添加它们,并且工具栏无法正确显示。基本上,我试图将上面的代码与类似的东西结合起来:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coord"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
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>
您可以通过将协调器布局存储在名为 coordinator_tabs 的单独文件中并将其包含在 DrawerLayout 中来干净地完成此操作。您的文件将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/navdrawer"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
如果您使用的是 Android Studio,您可以通过 right-clicking 在 java 文件夹中查看示例,然后选择新建->Activity->导航抽屉 Activity
我正在开发一个需要 ActionBar 选项卡和导航抽屉的应用程序。然而,这两者似乎在布局上相互冲突。对于导航抽屉,我需要有一个 DrawerLayout
作为根布局,然后是抽屉和内容的另外两个视图。这真的很容易,而且效果很好。执行此操作的代码是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/navdrawer"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
我可以说它甚至不需要 运行 或任何其他工作就可以工作:
问题是我需要 TabLayout
、ViewPager
和自定义 ToolBar
,所有这些都需要 CoordinatorLayout
作为根元素才能正常工作。无论如何我可以获得正常工作所需的三个视图,将 DrawerLayout
保持为根?我试过在没有 CoordinatorLayout
作为根视图的情况下添加它们,并且工具栏无法正确显示。基本上,我试图将上面的代码与类似的东西结合起来:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coord"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
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>
您可以通过将协调器布局存储在名为 coordinator_tabs 的单独文件中并将其包含在 DrawerLayout 中来干净地完成此操作。您的文件将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/navdrawer"
tools:context="io.github.ncca_fbla.fabric.MainActivity">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
如果您使用的是 Android Studio,您可以通过 right-clicking 在 java 文件夹中查看示例,然后选择新建->Activity->导航抽屉 Activity