【问题标题】:Toolbar not displaying from inside collapsing toolbar工具栏未从折叠工具栏内部显示
【发布时间】:2016-09-24 13:41:59
【问题描述】:

这里是代码。

<?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) 如果将 Viewpager 和 FAB 放在nestedScrollView 中,它们也不可见。 [已解决]

额外细节:
Viewpager 片段的布局以 Linearlayout 为根,内部有一个 recyclerview。

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

【问题讨论】:

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


    【解决方案1】:

    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) 如果将 Viewpager 和 FAB 放在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/

    希望对你有帮助

    【讨论】:

    • 已经想好第二和第三了。即使这样做了Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);,它也是第一个不起作用的
    • @DarshanMiskin 我更新了我的答案。您忘记设置默认工具栏高度 - wrap_content 在这种情况下可能等于零。属性 actionBarSize 设置 Android 标准工具栏大小。
    • @DarshanMiskin 将背景设置为工具栏,如android:background="?attr/colorPrimary",可能因为颜色而不可见:-D
    • 根据您提供的参考资料,发现在已经使用工具栏时不需要折叠工具栏。一切正常。
    【解决方案2】:

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

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

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

    【讨论】:

    • NestedScrollView 不是必需的,属性layout_behavior 很重要。看看这个stackoverflow.com/questions/31275277/…。此外,更改主题后,折叠工具栏不会折叠。现在将检查您的代码。
    • 更改主题后,折叠工具栏NOW正在折叠。
    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多