android – 工具栏无法从折叠工具栏中显示

这是代码.
<?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"
    android:id="@+id/main"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enteralways|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/mytoolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Light"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/main_tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_collapseMode="none"/>


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

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

    <!--<android.support.v4.widget.nestedScrollView-->
        <!--android:id="@+id/nestedscroll"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:fillViewport="true"-->
        <!--android:scrollbars="horizontal"-->
        <!--app:layout_behavior="@string/appbar_scrolling_view_behavior">-->

            <android.support.v4.view.ViewPager
                android:id="@+id/main_viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <!--</android.support.v4.widget.nestedScrollView>-->

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/main_fab"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/main_viewpager"
        app:layout_anchorGravity="bottom|end"/>

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

问题:
1)工具栏不可见.
2)折叠工具栏根本不会崩溃. [解决了]
3)如果放在nestedScrollView中,Viewpager和FAB也不可见. [解决了]

额外细节:
Viewpager片段的布局将Linearlayout作为root,内部具有recyclerview.

根据代码,一切似乎都没问题.无法理解缺少的东西.协调器布局和折叠工具栏如何协同工作的一个很好的解释也确实有帮助.

解决方法

1) Toolbar is not visible.

首先,您需要定义要在活动类中使用的工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

更改现有的xml代码

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:title="@string/app_name"
            app:layout_collapseMode="parallax">
        </android.support.v7.widget.Toolbar>

至:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"  //set initial height
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" //this might be also useful
            app:title="@string/app_name"
            app:layout_collapseMode="parallax" />

2) Collapsing toolbar doesn’t collapse at all.

您的活动使用了正确的主题吗?设置为AppBarLayout:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

如下例所示:include_list_viewpager.xml

3) Viewpager and FAB also not visible if put inside nestedScrollView.

没有理由这样做.添加这些行:

android:layout_marginTop="?attr/actionBarSize" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"

到ViewPager就足够了.

他们都应该是CoordinatorLayout的直接子女.

请遵循以下示例:http://blog.nkdroidsolutions.com/collapsing-toolbar-with-tabs-android-example/

如果您是Material Design的新手,或者对某些行为感到有些失落,我强烈建议您查看Chris Banes Material Design项目cheesequare:https://github.com/chrisbanes/cheesesquare/

希望它会有所帮助

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...