Android,抽屉布局+碎片+可折叠工具栏布局。

11

是否可以在DrawerLayout的主容器中显示的片段中使用CoordinatorLayout / CollapsingToolbarLayout?

对另一个问题的回答表明,每个片段都可以有自己的工具栏。但是这与ActionBarDrawerToggle不兼容,因为它需要一个工具栏来链接打开/关闭抽屉行为。

有人实现了这一点吗?或者您有关于此的指针?谢谢。

编辑:我一直在努力将单个工具栏放置在DrawerLayout中,以便始终保留,但无法使其滚动(在Nexus5 API 22上)。在this question中提到需要将CoordinatorLayout设置为主视图。因此,可能将其插入DrawerLayout(如下所示)是行不通的。

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

    <!-- main content -->
    <android.support.design.widget.CoordinatorLayout ...>

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

            <android.support.design.widget.CollapsingToolbarLayout ...>

                <ImageView .../>

                <android.support.v7.widget.Toolbar .../>

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

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

        <android.support.v7.widget.RecyclerView .../>

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

    <!-- navigation drawer -->
    <android.support.design.widget.NavigationView ...>

        <!-- drawer content -->
        <fragment .../>

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

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

为什么需要不同的工具栏,因为Android允许片段从其类内修改工具栏。因此,我建议您在片段内修改工具栏,而不是为每个片段使用不同的工具栏。 - EEJ
有趣...这正是我现在正在尝试做的事情。我会让你知道的。 - d4c0d312
3个回答

6
事实上,您可以。我也在寻找同样的内容,您问题中的链接带我来到了这里,这是来自Google Blogpost的内容。
以下是我的布局文件,至于Java代码,我没有做任何更改,Fragment调用保持不变。
activity_main.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:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml(导航栏布局)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <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_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/content_main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>

现在你有了"DrawerLayout + Fragments + CollapsingToolbarLayout"。

这是一个很好的建议。但是,如果您能帮我适应我的情况,我会非常感激。我还需要使用ViewPager来与TabLayout一起工作。我已经用TabLayout替换了CollapsedToolbar,这很好,但我不知道在哪里放置ViewPager,以便它不会干扰FrameLayout。 - iBobb
@iBobb,你能给我你的布局文件(activity_main.xml、app_bar_main.xml和content_main.xml),这样我就可以尝试一下你的情况。 - JustADev
嘿,感谢您的时间,我已经在这个问题上苦苦挣扎了几周。您可以在我的另一个问题中找到我的xml文件:http://stackoverflow.com/questions/35590799/avoid-viewpager-going-under-toolbar。目前我的ViewPager在主机片段的布局中(它加载在content_main_frame中),因此它不可见。然而,如果我尝试将它放在那三个布局文件中的任何一个地方,它根本就看不见。 - iBobb
@iBobb 抱歉,我会在这个周末检查一下,之前没能做到。 - JustADev
2
不用担心,我现在已经找到了一个解决方案,希望是最好的,所以现在一切都正常工作。我把它发布为我的问题的答案。无论如何,还是谢谢! - iBobb

2
这个布局对我很有效。
<?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:id="@+id/drawer_layout"
    android:layout_gravity="left"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".BaseDrawerActivity"
    android:foregroundGravity="left">


<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".myapp">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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


<!-- The drawer -->
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
    android:id="@+id/drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    style="@style/BaseStyle_Dark" />

按照正常流程初始化活动

public class BaseDrawerActivity extends AppCompatActivity implements MenuInterface {

private DrawerLayout mDrawerLayout;
private StickyListHeadersListView menuItemListView;
private static MenuItemAdapter menuItemAdapter;
private ActionBarDrawerToggle mDrawerToggle;

private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        menuItemListView = (StickyListHeadersListView) findViewById(R.id.drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (savedInstanceState == null) {

        }

        mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.home_open,R.string.home_close);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();

    }

这将允许您添加通常的CoordinatorLayout特性。

我终于成功地将导航栏+CoordinatorLayout+工具栏+片段+TabLayout+AppBarLayout结合起来,感谢这个答案、android4devs的NavigationDrawer教程和CodePath的NavigationDrawer教程。 - Avamander

0

我遇到了同样的问题,但是我不需要 ImageView,所以这是我的解决方案:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        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.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="@dimen/toolbar_elevation"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>

希望能对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接