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

时间:2016-09-24 13:42:00

标签: android android-layout android-xml android-coordinatorlayout android-collapsingtoolbarlayout

这是代码。

<?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,内部具有recyclelerview。

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

2 个答案:

答案 0 :(得分:4)

  

1)工具栏不可见。

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

 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)折叠工具栏根本不会崩溃。

您的活动是否使用了正确的主题。设置为AppBarLayout

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

,例如:include_list_viewpager.xml

  

3)如果放入nestedScrollView中,Viewpager和FAB也不可见。

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

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项目cheesequarehttps://github.com/chrisbanes/cheesesquare/

希望它会有所帮助

答案 1 :(得分:2)

首先,你应该告诉你的活动你正在使用什么工具栏,所以在onCreate方法中你应该有:

document.getElementById("outputLaps").outerHTML='';

你的第二个和第三个问题应该一起解决。您应该使用NestedScrollView作为ViewPager内部片段的主要布局,然后在其中放置LinearLayout或其他任何内容。